Skip to main content

CellBuffer

Struct CellBuffer 

Source
pub struct CellBuffer { /* private fields */ }
Expand description

Allocates and manages unsynchronized shared bytes.

Provides a utility to allocate a slice of bytes aligned to the maximally required alignment. Since the elements are much larger than single bytes the inner storage will not have exact sizes as one would be used from by using a Vec as an allocator. This is instead more close to a RawVec and most operations have the same drawback as Vec::reserve_exact in not actually being exact.

Since exact length and capacity semantics are hard to guarantee for most operations, no effort is made to uphold them. Instead. keeping track of the exact, wanted logical length of the requested byte slice is the obligation of the user under all circumstances. As a consequence, there are also no operations which explicitely uncouple length and capacity. All operations simply work on best effort of making some number of bytes available.

Implementations§

Source§

impl CellBuffer

Source

pub fn new(length: usize) -> Self

Allocate a new CellBuffer with a number of bytes.

Panics if the length is too long to find a properly aligned subregion.

Source

pub fn with_buffer(buffer: Buffer) -> Self

Share an existing buffer.

The library will try, to an extent, to avoid an allocation here. However, it can only do so if the capacity of the underlying buffer is the same as the logical length of the shared buffer. Ultimately we rely on the standard libraries guarantees for constructing a reference counted allocation from an owned vector.

Source

pub fn ptr_eq(&self, other: &Self) -> bool

Query if two buffers share the same memory region.

Source

pub fn capacity(&self) -> usize

Retrieve the byte capacity of the allocated storage.

Source

pub fn get_mut(&mut self) -> Option<&mut cell_buf>

Get this buffer if there are now copies.

use image_texel::texels::{AtomicBuffer, U8};

let mut buffer = AtomicBuffer::new(4);
assert!(buffer.get_mut().is_some());
let alias = buffer.clone();
assert!(buffer.get_mut().is_none());
Source

pub fn make_mut(&mut self) -> &mut cell_buf

Ensure this buffer is its own copy.

use image_texel::texels::{AtomicBuffer, U8};

let mut buffer = AtomicBuffer::new(4);
let mut alias = buffer.clone();

U8.store_atomic(buffer.as_texels(U8).index_one(0), 1);
let unshared = buffer.make_mut().as_buf_mut();
let alias = alias.get_mut().expect("Just unaliased");

unshared.as_mut_texels(U8)[0] = 2;
assert_eq!(alias.as_buf_mut().as_mut_texels(U8)[0], 1);
Source

pub fn to_owned(&self) -> Buffer

Copy the data into an owned buffer.

Source

pub fn to_resized(&self, bytes: usize) -> Self

Create an independent copy of the buffer, with a new length.

The prefix contents of the new buffer will be the same as the current buffer. The new buffer will never share memory with the current buffer.

Methods from Deref<Target = cell_buf>§

Source

pub fn len(&self) -> usize

Get the length of available memory in bytes.

Source

pub fn truncate(&self, at: usize) -> &Self

Reduce the number of bytes covered by this slice.

Source

pub fn split_at(&self, at: usize) -> (&Self, &Self)

Split into two aligned buffers.

§Panics

This panics if the byte offset given by at is not aligned according to max alignment or if the index is out-of-bounds.

Source

pub fn as_texels<P>(&self, texel: Texel<P>) -> &Cell<[P]>

Reinterpret the buffer for the specific texel type.

The alignment of P is already checked to be smaller than MAX_ALIGN through the constructor of Texel. The slice will have the maximum length possible but may leave unused bytes in the end.

Source

pub fn map_within<P, Q>( &self, src: impl RangeBounds<usize>, dest: usize, f: impl Fn(P) -> Q, p: Texel<P>, q: Texel<Q>, )

Apply a mapping function to some elements.

The indices src and dest are indices as if the slice were interpreted as [P] or [Q] respectively.

The types may differ which allows the use of this function to prepare a reinterpretation cast of a typed buffer. This function chooses the order of function applications such that values are not overwritten before they are used, i.e. the function arguments are exactly the previously visible values. This is even less trivial than for copy if the parameter types differ in size.

§Panics

This function panics if src or the implied range of dest are out of bounds.

Source

pub const ALIGNMENT: usize = MAX_ALIGN

Trait Implementations§

Source§

impl Clone for CellBuffer

Source§

fn clone(&self) -> CellBuffer

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Default for CellBuffer

Source§

fn default() -> CellBuffer

Returns the “default value” for a type. Read more
Source§

impl Deref for CellBuffer

Source§

type Target = cell_buf

The resulting type after dereferencing.
Source§

fn deref(&self) -> &cell_buf

Dereferences the value.
Source§

impl Eq for CellBuffer

Source§

impl From<&[u8]> for CellBuffer

Source§

fn from(values: &[u8]) -> Self

Converts to this type from the input type.
Source§

impl From<Buffer> for CellBuffer

Source§

fn from(values: Buffer) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for CellBuffer

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, L> PlaneOf<&L> for P
where P: PlaneOf<L>,

Source§

type Plane = <P as PlaneOf<L>>::Plane

Source§

fn get_plane(self, layout: &&L) -> Option<<P as PlaneOf<&L>>::Plane>

Get the layout describing the plane.
Source§

impl<P, L> PlaneOf<&mut L> for P
where P: PlaneOf<L>,

Source§

type Plane = <P as PlaneOf<L>>::Plane

Source§

fn get_plane(self, layout: &&mut L) -> Option<<P as PlaneOf<&mut L>>::Plane>

Get the layout describing the plane.
Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.