ownable_core/
as_copy.rs

1use crate::as_impl::impl_as;
2use crate::traits::{IntoOwned, ToBorrowed, ToOwned};
3use core::borrow::{Borrow, BorrowMut};
4use core::cmp::Ordering;
5use core::fmt::{Debug, Display, Formatter};
6use core::hash::{Hash, Hasher};
7use core::ops::{Deref, DerefMut};
8
9/// Transparent wrapper for [`Copy`]able types to support all traits (by copying).
10///
11/// ```rust
12/// # use ownable_core::{AsCopy, IntoOwned};
13/// #[derive(Clone, Copy)]
14/// struct NotIntoOwned;
15///
16/// fn requires_to_owned<O: IntoOwned>(o: O) {
17///   // do stuff
18/// }
19///
20/// // `AsCopy<NotIntoOwned>` implements `IntoOwned` through `Copy`
21/// requires_to_owned(AsCopy(NotIntoOwned));
22/// ```
23///
24/// All trait impls work on the inner value as if there is no layer in between
25/// (e.g. `Display` does not add a `AsCopy` to the output).
26#[repr(transparent)]
27pub struct AsCopy<T: Copy>(pub T);
28
29impl<T: Copy> ToBorrowed<'_> for AsCopy<T> {
30    #[inline(always)]
31    fn to_borrowed(&self) -> Self {
32        AsCopy(self.0)
33    }
34}
35
36impl<T: Copy> ToOwned for AsCopy<T> {
37    type Owned = AsCopy<T>;
38
39    #[inline(always)]
40    fn to_owned(&self) -> Self::Owned {
41        AsCopy(self.0)
42    }
43}
44
45impl<T: Copy> IntoOwned for AsCopy<T> {
46    type Owned = AsCopy<T>;
47
48    #[inline(always)]
49    fn into_owned(self) -> Self::Owned {
50        AsCopy(self.0)
51    }
52}
53
54impl_as!(AsCopy, Copy);