Skip to main content

WritebackGuard

Struct WritebackGuard 

Source
pub struct WritebackGuard<'a, T: Element + Clone, D: Dimension> { /* private fields */ }
Expand description

RAII writeback guard for safe out-parameter mutation.

See the module-level documentation for the usage pattern. The scratch is dropped without writing back unless Self::commit is called — this matches numpy’s WRITEBACKIFCOPY semantics where a kernel that panics or returns an error leaves the original untouched.

Implementations§

Source§

impl<'a, T: Element + Clone, D: Dimension> WritebackGuard<'a, T, D>

Source

pub fn new(target: &'a mut Array<T, D>) -> FerrayResult<Self>

Create a new writeback guard targeting target.

If target is already contiguous, the scratch is a clone of the target (ferray does not yet support borrowing the target’s buffer directly while keeping the lifetime invariant — every scratch is an owned Array). In either case the user works on the scratch and calls Self::commit to publish.

§Errors

Returns an error only if the underlying contiguous allocation fails for the target’s shape.

Source

pub fn scratch_mut(&mut self) -> &mut Array<T, D>

Mutable access to the contiguous scratch buffer. The kernel writes here; nothing is observable in the target until Self::commit is called.

Source

pub const fn scratch(&self) -> &Array<T, D>

Read access to the contiguous scratch (for kernels that need to read the input alongside the output write).

Source

pub fn commit(self) -> FerrayResult<()>

Publish the scratch contents back into the target. Element-by- element copy in logical (row-major) order, which works for any target layout (C-contiguous, F-contiguous, strided slice).

Consuming self so the guard can’t be used twice.

§Errors

Returns an error if the scratch and target sizes have somehow diverged (should be unreachable because the guard owns both).

Source

pub fn discard(self)

Discard the scratch without writing back. Equivalent to letting the guard go out of scope without calling Self::commit, but makes the intent explicit at the call site.

Auto Trait Implementations§

§

impl<'a, T, D> Freeze for WritebackGuard<'a, T, D>
where D: Freeze, <D as Dimension>::NdarrayDim: Freeze,

§

impl<'a, T, D> RefUnwindSafe for WritebackGuard<'a, T, D>

§

impl<'a, T, D> Send for WritebackGuard<'a, T, D>

§

impl<'a, T, D> Sync for WritebackGuard<'a, T, D>

§

impl<'a, T, D> Unpin for WritebackGuard<'a, T, D>
where D: Unpin, <D as Dimension>::NdarrayDim: Unpin,

§

impl<'a, T, D> UnsafeUnpin for WritebackGuard<'a, T, D>

§

impl<'a, T, D> !UnwindSafe for WritebackGuard<'a, T, D>

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> 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, 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.