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>
impl<'a, T: Element + Clone, D: Dimension> WritebackGuard<'a, T, D>
Sourcepub fn new(target: &'a mut Array<T, D>) -> FerrayResult<Self>
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.
Sourcepub fn scratch_mut(&mut self) -> &mut Array<T, D>
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.
Sourcepub const fn scratch(&self) -> &Array<T, D>
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).
Sourcepub fn commit(self) -> FerrayResult<()>
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).
Sourcepub fn discard(self)
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.