Skip to content

Commit 97586b6

Browse files
committed
Add A: Clone bound to into_shared
1 parent 307234e commit 97586b6

File tree

3 files changed

+33
-18
lines changed

3 files changed

+33
-18
lines changed

src/arraytraits.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,12 @@ use std::iter::IntoIterator;
1212
use std::mem;
1313
use std::ops::{Index, IndexMut};
1414
use alloc::boxed::Box;
15+
use alloc::sync::Arc;
1516
use alloc::vec::Vec;
1617

1718
use crate::imp_prelude::*;
1819
use crate::iter::{Iter, IterMut};
19-
use crate::NdIndex;
20-
21-
use crate::numeric_util;
22-
use crate::{FoldWhile, Zip};
20+
use crate::{numeric_util, FoldWhile, NdIndex, OwnedArcRepr, Zip};
2321

2422
#[cold]
2523
#[inline(never)]
@@ -372,7 +370,9 @@ where
372370
D: Dimension,
373371
{
374372
fn from(arr: Array<A, D>) -> ArcArray<A, D> {
375-
arr.into_shared()
373+
let data = OwnedArcRepr(Arc::new(arr.data));
374+
// safe because: equivalent unmoved data, ptr and dims remain valid
375+
unsafe { ArrayBase::from_data_ptr(data, arr.ptr).with_strides_dim(arr.strides, arr.dim) }
376376
}
377377
}
378378

src/data_traits.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ use std::ptr::NonNull;
1616
use alloc::sync::Arc;
1717
use alloc::vec::Vec;
1818

19-
use crate::{ArrayBase, CowRepr, Dimension, OwnedArcRepr, OwnedRepr, RawViewRepr, ViewRepr};
19+
use crate::{
20+
ArcArray, ArrayBase, CowRepr, Dimension, OwnedArcRepr, OwnedRepr, RawViewRepr, ViewRepr,
21+
};
2022

2123
/// Array representation trait.
2224
///
@@ -457,9 +459,13 @@ pub unsafe trait DataOwned: Data {
457459
fn new(elements: Vec<Self::Elem>) -> Self;
458460

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

465471
/// Array representation trait.
@@ -479,8 +485,12 @@ unsafe impl<A> DataOwned for OwnedRepr<A> {
479485
OwnedRepr::from(elements)
480486
}
481487

482-
fn into_shared(self) -> OwnedArcRepr<A> {
483-
OwnedArcRepr(Arc::new(self))
488+
fn into_shared<D>(self_: ArrayBase<Self, D>) -> ArcArray<A, D>
489+
where
490+
A: Clone,
491+
D: Dimension,
492+
{
493+
ArcArray::from(self_)
484494
}
485495
}
486496

@@ -491,8 +501,12 @@ unsafe impl<A> DataOwned for OwnedArcRepr<A> {
491501
OwnedArcRepr(Arc::new(OwnedRepr::from(elements)))
492502
}
493503

494-
fn into_shared(self) -> OwnedArcRepr<A> {
495-
self
504+
fn into_shared<D>(self_: ArrayBase<Self, D>) -> ArcArray<A, D>
505+
where
506+
A: Clone,
507+
D: Dimension,
508+
{
509+
self_
496510
}
497511
}
498512

src/impl_methods.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -242,16 +242,17 @@ where
242242
}
243243

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

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

0 commit comments

Comments
 (0)