pub struct Space<'a> { /* private fields */ }
Expand description
Specialized smart pointer to retrieve struct instances from a slice of memory.
The accessor methods of this struct all behave in a similar way: If the internal slice is big enough, they create a reference to the start of the slice with the desired type and create a new space object that contains the space after the references instance.
Implementations§
Source§impl<'a> Space<'a>
impl<'a> Space<'a>
Sourcepub unsafe fn from_atom(atom: &LV2_Atom) -> Self
pub unsafe fn from_atom(atom: &LV2_Atom) -> Self
Create a new space from an atom pointer.
The method creates a space that contains the atom as well as it’s body.
§Safety
Since the body is not included in the atom reference, this method has to assume that it is valid memory and therefore is unsafe but sound.
Sourcepub fn from_slice(data: &'a [u8]) -> Self
pub fn from_slice(data: &'a [u8]) -> Self
Create a new space from a slice.
Since everything regarding atoms is 64-bit-aligned, this method panics if the data slice is not 64-bit-aligned.
Sourcepub fn split_raw(self, size: usize) -> Option<(&'a [u8], Self)>
pub fn split_raw(self, size: usize) -> Option<(&'a [u8], Self)>
Try to retrieve a slice of bytes.
This method basically splits off the lower part of the internal bytes slice and creates a new atom space pointer of the upper part. Since atoms have to be 64-bit-aligned, there might be a padding space that’s neither in the lower nor in the upper part.
Sourcepub fn split_space(self, size: usize) -> Option<(Self, Self)>
pub fn split_space(self, size: usize) -> Option<(Self, Self)>
Try to retrieve space.
This method calls split_raw
and wraps the returned slice in an atom space. The second space is the space after the first one.
Sourcepub fn split_type<T>(self) -> Option<(&'a T, Self)>
pub fn split_type<T>(self) -> Option<(&'a T, Self)>
Try to retrieve a reference to a sized type.
This method retrieves a slice of memory using the split_raw
method and interprets it as an instance of T
. Since there is no way to check that the memory is actually a valid instance of T
, this method is unsafe. The second return value is the space after the instance of T
.
Sourcepub fn split_atom(self) -> Option<(Self, Self)>
pub fn split_atom(self) -> Option<(Self, Self)>
Try to retrieve the space occupied by an atom.
This method assumes that the space contains an atom and retrieves the space occupied by the atom, including the atom header. The second return value is the rest of the space behind the atom.
The difference to split_atom_body
is that the returned space contains the header of the atom and that the type of the atom is not checked.
Sourcepub fn split_atom_body<T: ?Sized>(self, urid: URID<T>) -> Option<(Self, Self)>
pub fn split_atom_body<T: ?Sized>(self, urid: URID<T>) -> Option<(Self, Self)>
Try to retrieve the body of the atom.
This method retrieves the header of the atom. If the type URID in the header matches the given URID, it returns the body of the atom. If not, it returns None
. The first space is the body of the atom, the second one is the space behind it.
The difference to split_atom
is that the returned space does not contain the header of the atom and that the type of the atom is checked.
Sourcepub fn from_reference<T: ?Sized>(instance: &'a T) -> Self
pub fn from_reference<T: ?Sized>(instance: &'a T) -> Self
Create a space from a reference.
Sourcepub fn concat(lhs: Self, rhs: Self) -> Option<Self>
pub fn concat(lhs: Self, rhs: Self) -> Option<Self>
Concatenate two spaces.
There are situations where a space is split too often and you might want to reunite these two adjacent spaces. This method checks if the given spaces are adjacent, which means that the left space has to end exactly where the right one begins. In this case, the concatenated space is returned. If this is not the case, this method returns None
.