Skip to main content

try_traits/
clone.rs

1//! Try traits for [`core::clone`].
2
3/// The try trait for [`Clone`].
4pub trait TryClone : Sized {
5	/// The type returned in the event of an error.
6	type Error;
7
8	/// The fallible equivalent of [`Clone::clone`].
9	fn try_clone(&self) -> Result<Self, Self::Error>;
10
11	/// The fallible equivalent of [`Clone::clone_from`].
12	#[inline]
13	fn try_clone_from(&mut self, source: &Self) -> Result<(), Self::Error> {
14		Ok(*self = source.try_clone()?)
15	}
16}
17
18impl<T: Clone> TryClone for T {
19	type Error = crate::Infallible;
20
21	#[inline]
22	fn try_clone(&self) -> Result<Self, Self::Error> {
23		Ok(self.clone())
24	}
25
26	#[inline]
27	fn try_clone_from(&mut self, source: &Self) -> Result<(), Self::Error> {
28		Ok(self.clone_from(source))
29	}
30}