@@ -10,16 +10,23 @@ use crate::loom::sync::atomic::AtomicMut;
10
10
use crate :: loom:: sync:: atomic:: { self , AtomicPtr , AtomicUsize , Ordering } ;
11
11
use crate :: Buf ;
12
12
13
- /// A reference counted contiguous slice of memory.
13
+ /// A cheaply cloneable and sliceable chunk of contiguous memory.
14
14
///
15
15
/// `Bytes` is an efficient container for storing and operating on contiguous
16
16
/// slices of memory. It is intended for use primarily in networking code, but
17
17
/// could have applications elsewhere as well.
18
18
///
19
19
/// `Bytes` values facilitate zero-copy network programming by allowing multiple
20
- /// `Bytes` objects to point to the same underlying memory. This is managed by
21
- /// using a reference count to track when the memory is no longer needed and can
22
- /// be freed.
20
+ /// `Bytes` objects to point to the same underlying memory.
21
+ ///
22
+ /// `Bytes` does not have a single implementation. It is an interface, whose
23
+ /// exact behavior is implemented through dynamic dispatch in several underlying
24
+ /// implementations of `Bytes`.
25
+ ///
26
+ /// All `Bytes` implementations must fulfill the following requirements:
27
+ /// - They are cheaply cloneable and thereby shareable between an unlimited amount
28
+ /// of components, for example by modifying a reference count.
29
+ /// - Instances can be sliced to refer to a subset of the the original buffer.
23
30
///
24
31
/// ```
25
32
/// use bytes::Bytes;
@@ -41,17 +48,33 @@ use crate::Buf;
41
48
/// to track information about which segment of the underlying memory the
42
49
/// `Bytes` handle has access to.
43
50
///
44
- /// `Bytes` keeps both a pointer to the shared `Arc` containing the full memory
51
+ /// `Bytes` keeps both a pointer to the shared state containing the full memory
45
52
/// slice and a pointer to the start of the region visible by the handle.
46
53
/// `Bytes` also tracks the length of its view into the memory.
47
54
///
48
55
/// # Sharing
49
56
///
50
- /// The memory itself is reference counted, and multiple `Bytes` objects may
51
- /// point to the same region. Each `Bytes` handle point to different sections within
52
- /// the memory region, and `Bytes` handle may or may not have overlapping views
57
+ /// `Bytes` contains a vtable, which allows implementations of `Bytes` to define
58
+ /// how sharing/cloneing is implemented in detail.
59
+ /// When `Bytes::clone()` is called, `Bytes` will call the vtable function for
60
+ /// cloning the backing storage in order to share it behind between multiple
61
+ /// `Bytes` instances.
62
+ ///
63
+ /// For `Bytes` implementations which refer to constant memory (e.g. created
64
+ /// via `Bytes::from_static()`) the cloning implementation will be a no-op.
65
+ ///
66
+ /// For `Bytes` implementations which point to a reference counted shared storage
67
+ /// (e.g. an `Arc<[u8]>`), sharing will be implemented by increasing the
68
+ /// the reference count.
69
+ ///
70
+ /// Due to this mechanism, multiple `Bytes` instances may point to the same
71
+ /// shared memory region.
72
+ /// Each `Bytes` instance can point to different sections within that
73
+ /// memory region, and `Bytes` instances may or may not have overlapping views
53
74
/// into the memory.
54
75
///
76
+ /// The following diagram visualizes a scenario where 2 `Bytes` instances make
77
+ /// use of an `Arc`-based backing storage, and provide access to different views:
55
78
///
56
79
/// ```text
57
80
///
@@ -175,7 +198,7 @@ impl Bytes {
175
198
self . len == 0
176
199
}
177
200
178
- ///Creates `Bytes` instance from slice, by copying it.
201
+ /// Creates `Bytes` instance from slice, by copying it.
179
202
pub fn copy_from_slice ( data : & [ u8 ] ) -> Self {
180
203
data. to_vec ( ) . into ( )
181
204
}
0 commit comments