Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add A: Clone bound to into_shared
  • Loading branch information
jturner314 authored and bluss committed Aug 2, 2024
commit be3aa20f837d8a247cfea417e80c379b63cb7b78
10 changes: 8 additions & 2 deletions src/arraytraits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,22 @@
use alloc::boxed::Box;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use std::hash;
use std::mem;
use std::mem::size_of;
use std::ops::{Index, IndexMut};
use std::{hash, mem::size_of};
use std::{iter::FromIterator, slice};

use crate::imp_prelude::*;
use crate::Arc;

use crate::{
dimension,
iter::{Iter, IterMut},
numeric_util,
FoldWhile,
NdIndex,
OwnedArcRepr,
Zip,
};

Expand Down Expand Up @@ -457,7 +461,9 @@ where D: Dimension
{
fn from(arr: Array<A, D>) -> ArcArray<A, D>
{
arr.into_shared()
let data = OwnedArcRepr(Arc::new(arr.data));
// safe because: equivalent unmoved data, ptr and dims remain valid
unsafe { ArrayBase::from_data_ptr(data, arr.ptr).with_strides_dim(arr.strides, arr.dim) }
}
}

Expand Down
22 changes: 16 additions & 6 deletions src/data_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -553,9 +553,13 @@ pub unsafe trait DataOwned: Data
fn new(elements: Vec<Self::Elem>) -> Self;

/// Converts the data representation to a shared (copy on write)
/// representation, without any copying.
/// representation, cloning the array elements if necessary.
#[doc(hidden)]
fn into_shared(self) -> OwnedArcRepr<Self::Elem>;
#[allow(clippy::wrong_self_convention)]
fn into_shared<D>(self_: ArrayBase<Self, D>) -> ArcArray<Self::Elem, D>
where
Self::Elem: Clone,
D: Dimension;
}

/// Array representation trait.
Expand All @@ -578,9 +582,12 @@ unsafe impl<A> DataOwned for OwnedRepr<A>
OwnedRepr::from(elements)
}

fn into_shared(self) -> OwnedArcRepr<A>
fn into_shared<D>(self_: ArrayBase<Self, D>) -> ArcArray<A, D>
where
A: Clone,
D: Dimension,
{
OwnedArcRepr(Arc::new(self))
ArcArray::from(self_)
}
}

Expand All @@ -593,9 +600,12 @@ unsafe impl<A> DataOwned for OwnedArcRepr<A>
OwnedArcRepr(Arc::new(OwnedRepr::from(elements)))
}

fn into_shared(self) -> OwnedArcRepr<A>
fn into_shared<D>(self_: ArrayBase<Self, D>) -> ArcArray<A, D>
where
A: Clone,
D: Dimension,
{
self
self_
}
}

Expand Down
14 changes: 9 additions & 5 deletions src/impl_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,13 +292,17 @@ where
}

/// Turn the array into a shared ownership (copy on write) array,
/// without any copying.
/// cloning the array elements if necessary.
///
/// If you want to generalize over `Array` and `ArcArray` inputs but avoid
/// an `A: Clone` bound, use `Into::<ArcArray<A, D>>::into` instead of this
/// method.
pub fn into_shared(self) -> ArcArray<A, D>
where S: DataOwned
where
A: Clone,
S: DataOwned,
{
let data = self.data.into_shared();
// safe because: equivalent unmoved data, ptr and dims remain valid
unsafe { ArrayBase::from_data_ptr(data, self.ptr).with_strides_dim(self.strides, self.dim) }
S::into_shared(self)
}

/// Returns a reference to the first element of the array, or `None` if it
Expand Down