Struct to_shmem::SharedMemoryBuilder
source · pub struct SharedMemoryBuilder {
pub(crate) buffer: *mut u8,
pub(crate) capacity: usize,
pub(crate) index: usize,
pub(crate) shared_values: HashSet<*const c_void>,
pub(crate) allowed_duplication_types: HashSet<TypeId>,
}
Expand description
A builder object that transforms and copies values into a fixed size buffer.
Fields§
§buffer: *mut u8
The buffer into which values will be copied.
capacity: usize
The size of the buffer.
index: usize
The current position in the buffer, where the next value will be written at.
Pointers to every sharable value that we store in the shared memory buffer. We use this to assert against encountering the same value twice, e.g. through another Arc reference, so that we don’t inadvertently store duplicate copies of values.
allowed_duplication_types: HashSet<TypeId>
Types of values that we may duplicate in the shared memory buffer when there are shared references to them, such as in Arcs.
Implementations§
sourcepub unsafe fn new(buffer: *mut u8, capacity: usize) -> SharedMemoryBuilder
pub unsafe fn new(buffer: *mut u8, capacity: usize) -> SharedMemoryBuilder
Creates a new SharedMemoryBuilder using the specified buffer.
sourcepub fn add_allowed_duplication_type<T: 'static>(&mut self)
pub fn add_allowed_duplication_type<T: 'static>(&mut self)
Notes a type as being allowed for duplication when being copied to the shared memory buffer, such as Arcs referencing the same value.
sourcepub fn write<T: ToShmem>(&mut self, value: &T) -> Result<*mut T, String>
pub fn write<T: ToShmem>(&mut self, value: &T) -> Result<*mut T, String>
Writes a value into the shared memory buffer and returns a pointer to it in the buffer.
The value is cloned and converted into a form suitable for placing into a shared memory buffer by calling ToShmem::to_shmem on it.
Panics if there is insufficient space in the buffer.
sourcepub fn alloc_value<T>(&mut self) -> *mut T
pub fn alloc_value<T>(&mut self) -> *mut T
Reserves space in the shared memory buffer to fit a value of type T, and returns a pointer to that reserved space.
Panics if there is insufficient space in the buffer.
sourcepub fn alloc_array<T>(&mut self, len: usize) -> *mut T
pub fn alloc_array<T>(&mut self, len: usize) -> *mut T
Reserves space in the shared memory buffer to fit an array of values of type T, and returns a pointer to that reserved space.
Panics if there is insufficient space in the buffer.