mirror of
https://github.com/tokio-rs/bytes.git
synced 2026-08-26 00:00:20 +02:00
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.
This commit is contained in:
+32
-9
@@ -10,16 +10,23 @@ use crate::loom::sync::atomic::AtomicMut;
|
|||||||
use crate::loom::sync::atomic::{self, AtomicPtr, AtomicUsize, Ordering};
|
use crate::loom::sync::atomic::{self, AtomicPtr, AtomicUsize, Ordering};
|
||||||
use crate::Buf;
|
use crate::Buf;
|
||||||
|
|
||||||
/// A reference counted contiguous slice of memory.
|
/// A cheaply cloneable and sliceable chunk of contiguous memory.
|
||||||
///
|
///
|
||||||
/// `Bytes` is an efficient container for storing and operating on contiguous
|
/// `Bytes` is an efficient container for storing and operating on contiguous
|
||||||
/// slices of memory. It is intended for use primarily in networking code, but
|
/// slices of memory. It is intended for use primarily in networking code, but
|
||||||
/// could have applications elsewhere as well.
|
/// could have applications elsewhere as well.
|
||||||
///
|
///
|
||||||
/// `Bytes` values facilitate zero-copy network programming by allowing multiple
|
/// `Bytes` values facilitate zero-copy network programming by allowing multiple
|
||||||
/// `Bytes` objects to point to the same underlying memory. This is managed by
|
/// `Bytes` objects to point to the same underlying memory.
|
||||||
/// using a reference count to track when the memory is no longer needed and can
|
///
|
||||||
/// be freed.
|
/// `Bytes` does not have a single implementation. It is an interface, whose
|
||||||
|
/// exact behavior is implemented through dynamic dispatch in several underlying
|
||||||
|
/// implementations of `Bytes`.
|
||||||
|
///
|
||||||
|
/// All `Bytes` implementations must fulfill the following requirements:
|
||||||
|
/// - They are cheaply cloneable and thereby shareable between an unlimited amount
|
||||||
|
/// of components, for example by modifying a reference count.
|
||||||
|
/// - Instances can be sliced to refer to a subset of the the original buffer.
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// use bytes::Bytes;
|
/// use bytes::Bytes;
|
||||||
@@ -41,17 +48,33 @@ use crate::Buf;
|
|||||||
/// to track information about which segment of the underlying memory the
|
/// to track information about which segment of the underlying memory the
|
||||||
/// `Bytes` handle has access to.
|
/// `Bytes` handle has access to.
|
||||||
///
|
///
|
||||||
/// `Bytes` keeps both a pointer to the shared `Arc` containing the full memory
|
/// `Bytes` keeps both a pointer to the shared state containing the full memory
|
||||||
/// slice and a pointer to the start of the region visible by the handle.
|
/// slice and a pointer to the start of the region visible by the handle.
|
||||||
/// `Bytes` also tracks the length of its view into the memory.
|
/// `Bytes` also tracks the length of its view into the memory.
|
||||||
///
|
///
|
||||||
/// # Sharing
|
/// # Sharing
|
||||||
///
|
///
|
||||||
/// The memory itself is reference counted, and multiple `Bytes` objects may
|
/// `Bytes` contains a vtable, which allows implementations of `Bytes` to define
|
||||||
/// point to the same region. Each `Bytes` handle point to different sections within
|
/// how sharing/cloneing is implemented in detail.
|
||||||
/// the memory region, and `Bytes` handle may or may not have overlapping views
|
/// When `Bytes::clone()` is called, `Bytes` will call the vtable function for
|
||||||
|
/// cloning the backing storage in order to share it behind between multiple
|
||||||
|
/// `Bytes` instances.
|
||||||
|
///
|
||||||
|
/// For `Bytes` implementations which refer to constant memory (e.g. created
|
||||||
|
/// via `Bytes::from_static()`) the cloning implementation will be a no-op.
|
||||||
|
///
|
||||||
|
/// For `Bytes` implementations which point to a reference counted shared storage
|
||||||
|
/// (e.g. an `Arc<[u8]>`), sharing will be implemented by increasing the
|
||||||
|
/// the reference count.
|
||||||
|
///
|
||||||
|
/// Due to this mechanism, multiple `Bytes` instances may point to the same
|
||||||
|
/// shared memory region.
|
||||||
|
/// Each `Bytes` instance can point to different sections within that
|
||||||
|
/// memory region, and `Bytes` instances may or may not have overlapping views
|
||||||
/// into the memory.
|
/// into the memory.
|
||||||
///
|
///
|
||||||
|
/// The following diagram visualizes a scenario where 2 `Bytes` instances make
|
||||||
|
/// use of an `Arc`-based backing storage, and provide access to different views:
|
||||||
///
|
///
|
||||||
/// ```text
|
/// ```text
|
||||||
///
|
///
|
||||||
@@ -175,7 +198,7 @@ impl Bytes {
|
|||||||
self.len == 0
|
self.len == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
///Creates `Bytes` instance from slice, by copying it.
|
/// Creates `Bytes` instance from slice, by copying it.
|
||||||
pub fn copy_from_slice(data: &[u8]) -> Self {
|
pub fn copy_from_slice(data: &[u8]) -> Self {
|
||||||
data.to_vec().into()
|
data.to_vec().into()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user