abi_stable/
traits.rs

1//! Where miscellaneous traits reside.
2
3use std::{borrow::Borrow, ops::Deref};
4
5#[allow(unused_imports)]
6use core_extensions::SelfOps;
7
8use crate::{
9    pointer_trait::{CanTransmuteElement, TransmuteElement},
10    sabi_types::{RMut, RRef},
11    std_types::{RSlice, RStr, RString, RVec},
12};
13
14///////////////////////////////////////////////////////////////////////////
15///////////////////////////////////////////////////////////////////////////
16///////////////////////////////////////////////////////////////////////////
17
18/// For cloning a reference-like type into a (preferably ffi-safe) owned type.
19pub trait IntoOwned: Copy + Deref {
20    /// The owned equivalent of this type.
21    type ROwned: Borrow<Self::Target>;
22
23    /// Performs the colne.
24    fn into_owned(self) -> Self::ROwned;
25}
26
27impl<T: Clone> IntoOwned for &T {
28    type ROwned = T;
29
30    fn into_owned(self) -> T {
31        self.clone()
32    }
33}
34
35impl IntoOwned for RStr<'_> {
36    type ROwned = RString;
37
38    fn into_owned(self) -> RString {
39        self.into()
40    }
41}
42
43impl<T: Clone> IntoOwned for RSlice<'_, T> {
44    type ROwned = RVec<T>;
45
46    fn into_owned(self) -> RVec<T> {
47        self.to_rvec()
48    }
49}
50
51///////////////////////////////////////////////////////////////////////////
52
53/// Converts a `#[repr(Rust)]` type into its `#[repr(C)]` equivalent.
54///
55/// `#[repr(Rust)]` is the default representation for data types.
56pub trait IntoReprC {
57    /// The `#[repr(C)]` equivalent.
58    type ReprC;
59
60    /// Performs the conversion
61    fn into_c(self) -> Self::ReprC;
62}
63
64/// Converts a `#[repr(C)]` type into its `#[repr(Rust)]` equivalent.
65///
66/// `#[repr(Rust)]` is the default representation for data types.
67pub trait IntoReprRust {
68    /// The `#[repr(Rust)]` equivalent.
69    type ReprRust;
70
71    /// Performs the conversion
72    fn into_rust(self) -> Self::ReprRust;
73}
74
75///////////////////////////////////////////////////////////////////////////
76///////////////////////////////////////////////////////////////////////////
77///////////////////////////////////////////////////////////////////////////
78
79macro_rules! impl_from_rust_repr {
80    (
81        $(#[$meta:meta])*
82        impl$([ $($impl_header:tt)* ])? From<$from_ty:ty> for $into_ty:ty
83        $( where [ $( $where_clause:tt )* ] )?
84        {
85            fn($this:pat) $function_contents:block
86        }
87
88
89    ) => (
90        $(#[$meta])*
91        impl $(< $($impl_header)* >)? From<$from_ty> for $into_ty
92        $(where $($where_clause)*)?
93        {
94            #[inline]
95            fn from($this:$from_ty)->$into_ty{
96                $function_contents
97            }
98        }
99
100        $(#[$meta])*
101        impl $(< $($impl_header)* >)?  $crate::traits::IntoReprC for $from_ty
102        $(where $($where_clause)*)?
103        {
104            type ReprC=$into_ty;
105            #[inline]
106            fn into_c(self)->Self::ReprC{
107                self.into()
108            }
109        }
110    )
111}
112
113macro_rules! impl_into_rust_repr {
114    (
115        $(#[$meta:meta])*
116        impl$([ $($impl_header:tt)* ])? Into<$into_ty:ty> for $from_ty:ty
117        $( where [ $( $where_clause:tt )* ] )?
118        {
119            fn($this:pat){
120                $($function_contents:tt)*
121            }
122        }
123
124    ) => (
125        $(#[$meta])*
126        impl $(< $($impl_header)* >)? From<$from_ty> for $into_ty
127        $(where $($where_clause)*)?
128        {
129            #[inline]
130            fn from($this: $from_ty) -> $into_ty{
131                $($function_contents)*
132            }
133        }
134
135        $(#[$meta])*
136        impl $(< $($impl_header)* >)?  $crate::traits::IntoReprRust for $from_ty
137        $(where $($where_clause)*)?
138        {
139            type ReprRust=$into_ty;
140            #[inline]
141            fn into_rust(self)->Self::ReprRust{
142                self.into()
143            }
144        }
145    )
146}
147
148///////////////////////////////////////////////////////////////////////////
149///////////////////////////////////////////////////////////////////////////
150///////////////////////////////////////////////////////////////////////////
151
152pub(crate) trait ErasedType<'a>: Sized {
153    type Unerased;
154
155    #[inline]
156    unsafe fn from_unerased<P>(p: P) -> P::TransmutedPtr
157    where
158        P: CanTransmuteElement<Self, PtrTarget = Self::Unerased>,
159    {
160        unsafe { p.transmute_element::<Self>() }
161    }
162
163    #[inline]
164    unsafe fn downcast_into<P>(p: P) -> P::TransmutedPtr
165    where
166        P: CanTransmuteElement<Self::Unerased, PtrTarget = Self>,
167    {
168        unsafe { p.transmute_element::<Self::Unerased>() }
169    }
170
171    #[inline]
172    unsafe fn run_downcast_as<'b, F, R>(p: RRef<'b, Self>, func: F) -> R
173    where
174        Self::Unerased: 'b,
175        F: FnOnce(&'b Self::Unerased) -> R,
176    {
177        unsafe { func(p.transmute_into_ref::<Self::Unerased>()) }
178    }
179
180    #[inline]
181    unsafe fn run_downcast_as_mut<'b, F, R>(p: RMut<'b, Self>, func: F) -> R
182    where
183        Self::Unerased: 'b,
184        F: FnOnce(&'b mut Self::Unerased) -> R,
185    {
186        unsafe { func(p.transmute_into_mut()) }
187    }
188}
189
190///////////////////////////////////////////////////////////////////////////
191
192/// Unwraps a type into its owned value.
193pub trait IntoInner {
194    /// The type of the value this owns.
195    type Element;
196
197    /// Unwraps this type into its owned value.
198    fn into_inner_(self) -> Self::Element;
199}
200
201///////////////////////////////////////////////////////////////////////////