SSlice

Struct SSlice 

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

An allocated block of stable memory.

Represented by a pointer to the first byte of the memory block and a u64 size of this block in bytes. It implements Copy, but using it after deallocation is undefined behavior.

In stable memory each memory block has the following layout:

  • bytes 0..8 - size + allocated bit flag (the flag uses the first bit of little endian encoded size)
  • bytes 8..(size + 8) - the data
  • bytes (size + 8)..(size + 16) - another size + allocated bit flag So, a memory block is simply size bytes of data wrapped with some metadata from both sides. FreeBlock is stored exactly in a same way.

Implementations§

Source§

impl SSlice

Source

pub unsafe fn from_ptr(ptr: StablePtr) -> Option<Self>

Recreate an SSlice from a pointer to the front of the memory block.

See also SSlice::from_rear_ptr.

This call will check whether a pointer is valid (points to an allocated memory block) and if it’s not, it will return None.

§Safety

By calling this function, you’re basically create a copy of an SSlice, be careful

Source

pub unsafe fn from_rear_ptr(ptr: StablePtr) -> Option<Self>

Recreate an SSlice from a pointer to the back of the memory block.

See also SSlice::from_ptr.

§Safety

By calling this function, you’re basically create a copy of an SSlice, be careful

Source

pub fn as_ptr(&self) -> StablePtr

Returns a pointer to the memory block.

Don’t use this function to point to the data inside this memory block! Use SSlice::offset instead.

Source

pub fn get_size_bytes(&self) -> u64

Returns the size of the data in this memory block in bytes.

Source

pub fn get_total_size_bytes(&self) -> u64

Returns the size of the whole memory block in bytes (including metadata).

Source

pub fn _offset(self_ptr: u64, offset: u64) -> StablePtr

Static analog of SSlice::offset.

Does not perform boundary check.

Source

pub fn offset(&self, offset: u64) -> StablePtr

Returns a pointer to the data inside SSlice.

One should use this function to write data in a memory block by using [mem::write_fixed] or [mem::write_bytes].

§Panics

Panics if boundary check fails (if the offset is outside the memory block).

§Example
let slice = unsafe { allocate(100).expect("Out of memory") };
let ptr = slice.offset(20);

// will write `10` as little endian bytes into the memory block
// starting from 20th byte
unsafe { mem::write_fixed(ptr, &mut 10u64); }

Trait Implementations§

Source§

impl Clone for SSlice

Source§

fn clone(&self) -> SSlice

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for SSlice

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Copy for SSlice

Auto Trait Implementations§

§

impl Freeze for SSlice

§

impl RefUnwindSafe for SSlice

§

impl Send for SSlice

§

impl Sync for SSlice

§

impl Unpin for SSlice

§

impl UnwindSafe for SSlice

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<T> Same for T

Source§

type Output = T

Should always be Self
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.