Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method implicit_clone() to help ensure the type is ImplicitClone #44

Merged
merged 2 commits into from
Nov 9, 2023
Merged
Changes from all commits
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
32 changes: 31 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,37 @@ pub mod unsync;
///
/// Enables host libraries to have the same syntax as [`Copy`] while calling the [`Clone`]
/// implementation instead.
pub trait ImplicitClone: Clone {}
pub trait ImplicitClone: Clone {
/// This function is not magic; it is literally defined as
///
/// ```ignore
/// fn implicit_clone(&self) -> Self {
/// self.clone()
/// }
/// ```
///
/// It is useful when you want to clone but also ensure that the type implements
/// [`ImplicitClone`].
///
/// Examples:
///
/// ```
/// use implicit_clone::ImplicitClone;
/// let x: u32 = Default::default();
/// let clone = ImplicitClone::implicit_clone(&x);
/// ```
///
/// ```compile_fail
/// use implicit_clone::ImplicitClone;
/// let x: Vec<u32> = Default::default();
/// // does not compile because Vec<_> does not implement ImplicitClone
/// let clone = ImplicitClone::implicit_clone(&x);
/// ```
#[inline]
fn implicit_clone(&self) -> Self {
self.clone()
}
}

impl<T: ?Sized> ImplicitClone for &T {}

Expand Down