Skip to main content

WellDefinedCopyable

Struct WellDefinedCopyable 

Source
pub struct WellDefinedCopyable<T: Copy + FromBytes + IntoBytes + Immutable> { /* private fields */ }
Expand description

Wrapper for transferring trivially copyable data into and out of shared memory using well-defined atomic operations.

Users wrap a type T in WellDefinedCopyable<T> and use Self::update and Self::read to copy data into and out of the contained T instance, respectively. These methods deliberately restrict access to the underlying storage so transfers occur through the lowest-level well-defined copy functions.

T must implement [Copy], FromBytes, IntoBytes, and Immutable to guarantee that it has no uninitialized padding bytes and that any byte pattern observed during a concurrent transfer is a valid representation of T. In addition, align_of::<T>() must be at least MAX_TRANSFER_GRANULARITY (8 bytes) so that source and destination buffers always share identical alignment modulo 8.

Implementations§

Source§

impl<T: Copy + FromBytes + IntoBytes + Immutable> WellDefinedCopyable<T>

Source

pub const fn new(instance: T) -> Self

Creates a new WellDefinedCopyable wrapping instance.

Source

pub fn read<const SYNC_OPT: u8>(&self, dst: &mut T)

Read from the wrapped object into the destination buffer provided by the caller.

Source

pub fn update<const SYNC_OPT: u8>(&self, src: &T)

Update the wrapped object from the source buffer provided by the caller.

Source

pub const fn unsynchronized_get(&self) -> *const T

WARNING: There be dragons here!

unsynchronized_get returns a raw pointer providing direct read-only access to the underlying instance of T. Dereferencing the pointer is only safe if the user can guarantee that no write operations may be concurrently performed against the storage while reading the instance.

One example of a legitimate use of this method might be when a user is operating in the write exclusive portion of a sequence lock. They are guaranteed to be the only potential writer of the wrapped object, so while it is still important that they continue to use update when they wish to mutate their instance of T, it is OK for them to read T directly without using read as this will not cause any undefined behavior when done concurrently with other readers in the system.

Returning a raw pointer *const T rather than a reference &T avoids Rust’s aliasing requirement that the pointee remain immutable for the entire lifetime of a reference, matching C++ where holding a reference across concurrent writes is permitted as long as it is not read during a write.

Trait Implementations§

Source§

impl<T: Copy + FromBytes + IntoBytes + Immutable + Default> Default for WellDefinedCopyable<T>

Source§

fn default() -> Self

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

impl<T: Copy + FromBytes + IntoBytes + Immutable + Send> Sync for WellDefinedCopyable<T>

Auto Trait Implementations§

§

impl<T> !Freeze for WellDefinedCopyable<T>

§

impl<T> !RefUnwindSafe for WellDefinedCopyable<T>

§

impl<T> Send for WellDefinedCopyable<T>
where T: Send,

§

impl<T> Unpin for WellDefinedCopyable<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for WellDefinedCopyable<T>
where T: UnsafeUnpin,

§

impl<T> UnwindSafe for WellDefinedCopyable<T>
where T: UnwindSafe,

Blanket Implementations§

§

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

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

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

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

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

§

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

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

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

§

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> ToMutPtr for T
where T: ?Sized,

Source§

type Target = T

The target type of the pointer.
Source§

fn to_mut_ptr(&self) -> *mut T

Casts the reference to a mutable raw pointer.
§

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

§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
§

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

§

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

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

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

Performs the conversion.