Skip to content

Commit 5a11c78

Browse files
authored
De-emphasize Arc implementation in Bytes description (#436)
The previous description focussed a lot on the `Arc` based implementation of `Bytes`. Given the vtable based implemetation, this is however not the only valid implementation. This changes the description a bit in order to de-emaphasize the `Arc` part, and to describe that other implementations are possible. This should also be necessary if the vtable gets public.
1 parent 4724c7e commit 5a11c78

File tree

1 file changed

+32
-9
lines changed

1 file changed

+32
-9
lines changed

src/bytes.rs

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,23 @@ use crate::loom::sync::atomic::AtomicMut;
1010
use crate::loom::sync::atomic::{self, AtomicPtr, AtomicUsize, Ordering};
1111
use crate::Buf;
1212

13-
/// A reference counted contiguous slice of memory.
13+
/// A cheaply cloneable and sliceable chunk of contiguous memory.
1414
///
1515
/// `Bytes` is an efficient container for storing and operating on contiguous
1616
/// slices of memory. It is intended for use primarily in networking code, but
1717
/// could have applications elsewhere as well.
1818
///
1919
/// `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.
2330
///
2431
/// ```
2532
/// use bytes::Bytes;
@@ -41,17 +48,33 @@ use crate::Buf;
4148
/// to track information about which segment of the underlying memory the
4249
/// `Bytes` handle has access to.
4350
///
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
4552
/// slice and a pointer to the start of the region visible by the handle.
4653
/// `Bytes` also tracks the length of its view into the memory.
4754
///
4855
/// # Sharing
4956
///
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
5374
/// into the memory.
5475
///
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:
5578
///
5679
/// ```text
5780
///
@@ -175,7 +198,7 @@ impl Bytes {
175198
self.len == 0
176199
}
177200

178-
///Creates `Bytes` instance from slice, by copying it.
201+
/// Creates `Bytes` instance from slice, by copying it.
179202
pub fn copy_from_slice(data: &[u8]) -> Self {
180203
data.to_vec().into()
181204
}

0 commit comments

Comments
 (0)