Skip to content

Commit b6de6ec

Browse files
Merge pull request #14 from triblespace/codex/add-module-level-comment-for-bytesource
Document built-in ByteSources
2 parents 15e253b + d9a6b05 commit b6de6ec

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@
66
- move weak reference and downcasting examples into module docs
77
- expand module introduction describing use cases
88
- document rationale for separating `ByteSource` and `ByteOwner`
9+
- summarize built-in `ByteSource`s and show how to extend them
910
- clarify library overview and development instructions in README

src/sources.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,36 @@
66
* LICENSE file in the root directory of this source tree.
77
*/
88

9+
//! Implementations of [`ByteSource`] for common byte containers.
10+
//!
11+
//! | Feature | Implementations |
12+
//! | ------------ | ---------------------------------------------------------------- |
13+
//! | `zerocopy` | `&'static [T]`, `Box<T>` and `Vec<T>` for `T: IntoBytes + Immutable` |
14+
//! | *(none)* | `&'static [u8]`, `Box<[u8]>`, `Vec<u8>`, `String`, `&'static str` |
15+
//! | `bytes` | `bytes::Bytes` |
16+
//! | `ownedbytes` | `ownedbytes::OwnedBytes` |
17+
//! | `mmap` | `memmap2::Mmap` and `ByteOwner` for `memmap2::MmapRaw` |
18+
//! | `pyo3` | `pyo3::Bound<'_, PyBytes>` and `ByteOwner` for `Py<PyBytes>` |
19+
//!
20+
//! To store bytes in your own type, implement [`ByteSource`].
21+
//! [`ByteOwner`] is provided automatically for all `ByteSource`s but can be
22+
//! implemented manually if needed:
23+
//!
24+
//! ```rust
25+
//! use anybytes::{ByteSource, Bytes};
26+
//!
27+
//! struct MyData(Vec<u8>);
28+
//!
29+
//! unsafe impl ByteSource for MyData {
30+
//! type Owner = Self;
31+
//!
32+
//! fn as_bytes(&self) -> &[u8] { &self.0 }
33+
//! fn get_owner(self) -> Self::Owner { self }
34+
//! }
35+
//!
36+
//! # let _ = Bytes::from_source(MyData(vec![1, 2, 3]));
37+
//! ```
38+
939
use zerocopy::Immutable;
1040
#[cfg(feature = "zerocopy")]
1141
use zerocopy::IntoBytes;

0 commit comments

Comments
 (0)