1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
//! Provides a way to efficiently get an owned value of a type.
//!
//! ```
//! use into_owned::{IntoOwned, Owned};
//!
//! fn check<S: IntoOwned<str>>(s: S) -> String {
//! s.into_owned()
//! }
//!
//! let _foo = check("foo"); // will clone
//! let _bar = check(Owned("bar".to_owned())); // will not clone twice (unlike regular ToOwned)
//! ```
//!
//! ```
//! # use std::borrow::Cow;
//! use into_owned::{IntoOwned, Owned};
//!
//! /// A simple type that increments an internal counter on clone
//! pub struct Counter(usize);
//! impl Clone for Counter {
//! fn clone(&self) -> Self {
//! Self(self.0 + 1)
//! }
//! }
//!
//! fn check<S: IntoOwned<Counter>>(s: S) -> Counter {
//! s.into_owned()
//! }
//!
//! let count = check(&Counter(0)); // pass by ref
//! assert_eq!(count.0, 1); // counter was cloned.
//!
//! let count = check(Owned(Counter(0))); // pass by value
//! assert_eq!(count.0, 0); // counter was not cloned.
//!
//! let count = check(Cow::Borrowed(&Counter(0))); // pass by ref (Cow)
//! assert_eq!(count.0, 1); // counter was cloned.
//!
//! let count = check(Cow::Owned(Counter(0))); // pass by value (Cow)
//! assert_eq!(count.0, 0); // counter was not cloned.
//! ```
use std::borrow::{Borrow, Cow};
mod owned;
pub use owned::Owned;
/// Extension to [`ToOwned`]. Avoiding clones where possible
pub trait IntoOwned<T: ?Sized + ToOwned>: Borrow<T> {
fn into_owned(self) -> T::Owned;
}
/// Default implemetation for references.
/// [`IntoOwned::into_owned()`] will perform [`ToOwned::to_owned()`]
impl<T: ToOwned + ?Sized> IntoOwned<T> for &T {
fn into_owned(self) -> T::Owned {
self.to_owned()
}
}
/// Default implemetation for Cow.
/// [`IntoOwned::into_owned()`] will perform [`Cow::into_owned()`]
impl<'a, T: ToOwned + ?Sized> IntoOwned<T> for Cow<'a, T> {
fn into_owned(self) -> <T as ToOwned>::Owned {
self.into_owned()
}
}