Skip to content

Commit

Permalink
update mirroring function
Browse files Browse the repository at this point in the history
  • Loading branch information
imrn99 committed Nov 10, 2023
1 parent 77d558f commit 1543e5c
Showing 1 changed file with 57 additions and 39 deletions.
96 changes: 57 additions & 39 deletions src/view/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use std::{
/// In all variants, the internal value is a description of the error.
pub enum ViewError<'a> {
ValueError(&'a str),
DoubleMirroring(&'a str),
}

#[derive(Debug)]
Expand Down Expand Up @@ -84,64 +85,81 @@ where
stride,
}
}
}

impl<'a, const N: usize, T> ViewBase<'a, N, T> {
// ~~~~~~~~ Uniform writing interface across all features

#[inline(always)]
#[cfg(not(any(feature = "rayon", feature = "threads")))]
/// Serial writing interface. Uses mutable indexing implementation.
pub fn set(&mut self, index: [usize; N], val: T) {
self[index] = val;
}

#[inline(always)]
#[cfg(any(feature = "rayon", feature = "threads"))]
/// Thread-safe writing interface. Uses non-mutable indexing and
/// immutability of atomic type methods.
pub fn set(&self, index: [usize; N], val: T) {
self[index].store(val, atomic::Ordering::Relaxed);
}

// ~~~~~~~~ Mirrors

pub fn create_mirror<'b>(&'a self) -> ViewRO<'b, N, T>
/// Create a new View mirroring `self`, i.e. referencing the same data. This mirror
/// is always immutable, but it inner values might still be writable if they are
/// atomic types.
///
/// Note that mirrors currently can only be created from the "original" view,
/// i.e. the view owning the data.
pub fn create_mirror<'b>(&'a self) -> Result<ViewRO<'b, N, T>, ViewError>
where
'a: 'b, // 'a outlives 'b
{
let inner: &[T] = match &self.data {
DataType::Owned(v) => &v[..],
DataType::Borrowed(slice) => slice,
DataType::MutBorrowed(mut_slice) => mut_slice, // is this allowed ?
let inner = if let DataType::Owned(v) = &self.data {
v
} else {
return Err(ViewError::DoubleMirroring(
"Cannot create a mirror from a non-data-owning View",
));
};
let data = DataType::Borrowed(inner);

Self {
data,
Ok(Self {
data: DataType::Borrowed(inner),
layout: self.layout,
dim: self.dim,
stride: self.stride,
}
})
}

pub fn create_mutable_mirror<'b>(&'a mut self) -> ViewRW<'b, N, T>
#[cfg(not(any(feature = "rayon", feature = "threads")))]
/// Only defined when no feature are enabled since all interfaces should be immutable
/// otherwise.
///
/// Create a new View mirroring `self`, i.e. referencing the same data. This mirror
/// uses a mutable reference, hence the serial-only definition
///
/// Note that mirrors currently can only be created from the "original" view,
/// i.e. the view owning the data.
pub fn create_mutable_mirror<'b>(&'a mut self) -> Result<ViewRW<'b, N, T>, ViewError>
where
'a: 'b, // 'a outlives 'b
{
let inner: &mut [T] = match &mut self.data {
DataType::Owned(v) => &mut v[..],
DataType::Borrowed(_) => {
unimplemented!("Cannot create a mutable mirror from a read-only view!")
}
DataType::MutBorrowed(mut_slice) => mut_slice, // is this allowed ?
let inner = if let DataType::Owned(v) = &mut self.data {
v
} else {
return Err(ViewError::DoubleMirroring(
"Cannot create a mirror from a non-data-owning View",
));
};
let data = DataType::Borrowed(inner);

Self {
data,
Ok(Self {
data: DataType::MutBorrowed(inner),
layout: self.layout,
dim: self.dim,
stride: self.stride,
}
}
}

impl<'a, const N: usize, T> ViewBase<'a, N, T> {
// ~~~~~~~~ Uniform writing interface across all features

#[inline(always)]
#[cfg(not(any(feature = "rayon", feature = "threads")))]
/// Serial writing interface. Uses mutable indexing implementation.
pub fn set(&mut self, index: [usize; N], val: T) {
self[index] = val;
}

#[inline(always)]
#[cfg(any(feature = "rayon", feature = "threads"))]
/// Thread-safe writing interface. Uses non-mutable indexing and
/// immutability of atomic type methods.
pub fn set(&self, index: [usize; N], val: T) {
self[index].store(val, atomic::Ordering::Relaxed);
})
}

// ~~~~~~~~ Convenience
Expand Down

0 comments on commit 1543e5c

Please sign in to comment.