try_clone/
alloc.rs

1#[cfg(all(feature = "alloc", not(feature = "std")))]
2extern crate alloc;
3#[cfg(feature = "std")]
4extern crate std as alloc;
5
6#[cfg(any(feature = "blanket-impl", feature = "nightly"))]
7use crate::TryClone;
8
9#[cfg(feature = "nightly")]
10use alloc::{
11    alloc::Allocator,
12    boxed::Box,
13    collections::{BinaryHeap, TryReserveError, VecDeque},
14    rc::Rc,
15    sync::Arc,
16    vec::Vec,
17};
18
19#[cfg(feature = "nightly")]
20use alloc::alloc::AllocError;
21
22#[cfg(feature = "blanket-impl")]
23use crate::ForwardTryCloneToClone;
24
25#[cfg(feature = "blanket-impl")]
26impl<T: ?Sized, A> !ForwardTryCloneToClone for Box<T, A> {}
27#[cfg(feature = "nightly")]
28#[cfg(not(feature = "blanket-impl"))]
29impl<T: Clone, A: Allocator + Clone> TryClone for Box<T, A> {
30    type Error = AllocError;
31
32    fn try_clone(&self) -> Result<Self, Self::Error> {
33        let alloc = Self::allocator(self).clone();
34        Self::try_clone_from_ref_in(self, alloc)
35    }
36}
37
38#[cfg(feature = "blanket-impl")]
39impl<T: ?Sized, A> !ForwardTryCloneToClone for Arc<T, A> {}
40#[cfg(feature = "nightly")]
41impl<T: Clone, A: Allocator + Clone> TryClone for Arc<T, A> {
42    type Error = AllocError;
43
44    fn try_clone(&self) -> Result<Self, Self::Error> {
45        let alloc = Self::allocator(self).clone();
46        Self::try_clone_from_ref_in(self, alloc)
47    }
48}
49
50#[cfg(feature = "blanket-impl")]
51impl<T: ?Sized, A> !ForwardTryCloneToClone for Rc<T, A> {}
52#[cfg(feature = "nightly")]
53impl<T: Clone, A: Allocator + Clone> TryClone for Rc<T, A> {
54    type Error = AllocError;
55
56    fn try_clone(&self) -> Result<Self, Self::Error> {
57        let alloc = Self::allocator(self).clone();
58        Self::try_clone_from_ref_in(self, alloc)
59    }
60}
61
62#[cfg(feature = "blanket-impl")]
63impl<T: ?Sized, A> !ForwardTryCloneToClone for Vec<T, A> {}
64#[cfg(feature = "nightly")]
65impl<T: Clone, A: Allocator + Clone> TryClone for Vec<T, A> {
66    type Error = TryReserveError;
67
68    fn try_clone(&self) -> Result<Self, Self::Error> {
69        let alloc = self.allocator().clone();
70        let mut cloned = Self::try_with_capacity_in(self.len(), alloc)?;
71        cloned.extend(self.iter().cloned());
72        Ok(cloned)
73    }
74}
75
76#[cfg(feature = "blanket-impl")]
77impl<T: ?Sized, A> !ForwardTryCloneToClone for VecDeque<T, A> {}
78#[cfg(feature = "nightly")]
79impl<T: Clone, A: Allocator + Clone> TryClone for VecDeque<T, A> {
80    type Error = TryReserveError;
81
82    fn try_clone(&self) -> Result<Self, Self::Error> {
83        let alloc = self.allocator().clone();
84        let mut cloned = Self::new_in(alloc);
85        cloned.try_reserve(self.len())?;
86        cloned.extend(self.iter().cloned());
87        Ok(cloned)
88    }
89}
90
91#[cfg(feature = "blanket-impl")]
92impl<T: ?Sized, A> !ForwardTryCloneToClone for BinaryHeap<T, A> {}
93#[cfg(feature = "nightly")]
94impl<T: Clone + Ord, A: Allocator + Clone> TryClone for BinaryHeap<T, A> {
95    type Error = TryReserveError;
96
97    fn try_clone(&self) -> Result<Self, Self::Error> {
98        let alloc = self.allocator().clone();
99        let mut cloned = Self::new_in(alloc);
100        cloned.try_reserve(self.len())?;
101        cloned.extend(self.iter().cloned());
102        Ok(cloned)
103    }
104}