pub struct Buffer<T: Clone> { /* private fields */ }Expand description
A reference-counted buffer with copy-on-write semantics.
Internally backed by Rc<RefCell<Vec<T>>>. When multiple Buffers share
the same underlying storage, a mutation via Buffer::set or
Buffer::make_unique triggers a deep copy so that other holders are
unaffected.
Implementations§
Source§impl<T: Clone> Buffer<T>
impl<T: Clone> Buffer<T>
Sourcepub fn alloc(len: usize, default: T) -> Self
pub fn alloc(len: usize, default: T) -> Self
Allocate a buffer of len elements, each initialized to default.
Sourcepub fn get(&self, idx: usize) -> Option<T>
pub fn get(&self, idx: usize) -> Option<T>
Read the value at idx. Returns None if out of bounds.
Sourcepub fn set(&mut self, idx: usize, val: T) -> Result<(), RuntimeError>
pub fn set(&mut self, idx: usize, val: T) -> Result<(), RuntimeError>
Write val at idx. If the buffer is shared (refcount > 1), a deep
copy is performed first (COW). Returns Err if idx is out of bounds.
Sourcepub fn borrow_data(&self) -> Ref<'_, Vec<T>>
pub fn borrow_data(&self) -> Ref<'_, Vec<T>>
Borrow the underlying Vec without cloning.
The returned Ref guard keeps the borrow alive.
Sourcepub fn clone_buffer(&self) -> Buffer<T>
pub fn clone_buffer(&self) -> Buffer<T>
Force a deep copy, returning a new Buffer that does not share
storage with self.
Sourcepub fn make_unique(&mut self)
pub fn make_unique(&mut self)
Ensure this Buffer has exclusive ownership of the underlying data.
If the refcount is > 1, the data is deep-copied and this Buffer is
re-pointed to the fresh copy.
Trait Implementations§
Auto Trait Implementations§
impl<T> Freeze for Buffer<T>
impl<T> !RefUnwindSafe for Buffer<T>
impl<T> !Send for Buffer<T>
impl<T> !Sync for Buffer<T>
impl<T> Unpin for Buffer<T>
impl<T> UnsafeUnpin for Buffer<T>
impl<T> !UnwindSafe for Buffer<T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more