proc_bitfield/
conv.rs

1#![allow(clippy::missing_safety_doc)]
2
3#[cfg(not(feature = "nightly"))]
4mod impls;
5#[cfg(feature = "nightly")]
6mod impls_nightly;
7
8const_trait! {
9    /// Equivalent of [`core::ops::Try`] that doesn't require nightly and allows changing the output
10    /// type using a GAT.
11    ///
12    /// Automatically implemented for `Option<T>` and `Result<T, E>`. (Logically depends on
13    /// [`core::ops::Try`], but doesn't have an explicit dependency as it's unstable).
14    pub trait Try {
15        type Output;
16        type WithOutput<T>: Try;
17
18        fn from_output(output: Self::Output) -> Self;
19    }
20}
21
22const_trait! {
23    /// Unsafe equivalent of [`From`].
24    ///
25    /// Used to do unsafe value-to-value conversions while consuming the input value. It is the
26    /// reciprocal of [`UnsafeInto`].
27    ///
28    /// One should always prefer implementing `UnsafeFrom` over [`UnsafeInto`] because implementing
29    /// `UnsafeFrom` automatically provides one with an implementation of [`UnsafeInto`] thanks to
30    /// the blanket implementation.
31    ///
32    /// Prefer using [`UnsafeInto`] over using `UnsafeFrom` when specifying trait bounds on a
33    /// generic function. This way, types that directly implement [`UnsafeInto`] can be used as
34    /// arguments as well.
35    ///
36    /// **Note: This trait must not fail**. The `UnsafeFrom` trait is intended for unsafe but
37    /// perfect conversions. If the conversion can fail or is not perfect, use [`TryFrom`].
38    ///
39    /// # Generic Implementations
40    ///
41    /// - `UnsafeFrom<T> for U` implies [`UnsafeInto`]`<U> for T`
42    /// - `UnsafeFrom`, like [`From`], is reflexive, which means that `UnsafeFrom<T> for T` is
43    ///   implemented
44    /// - [`From`]`<T> for U` implies `UnsafeFrom<T> for U`
45    /// - [`Into`]`<U> for T` implies `UnsafeFrom<T> for U`
46    pub trait UnsafeFrom<T> {
47        /// Unsafely converts to this type from the input type.
48        unsafe fn unsafe_from(_: T) -> Self;
49    }
50}
51
52const_trait! {
53    /// Unsafe equivalent of [`Into`].
54    ///
55    /// Used to do unsafe value-to-value conversions while consuming the input value. It is the
56    /// reciprocal of [`UnsafeFrom`].
57    ///
58    /// One should always prefer implementing [`UnsafeFrom`] over `UnsafeInto` because implementing
59    /// [`UnsafeFrom`] automatically provides one with an implementation of `UnsafeInto` thanks to
60    /// the blanket implementation.
61    ///
62    /// Prefer using `UnsafeInto` over using [`UnsafeFrom`] when specifying trait bounds on a
63    /// generic function. This way, types that directly implement `UnsafeInto` can be used as
64    /// arguments as well.
65    ///
66    /// **Note: This trait must not fail**. The `UnsafeInto` trait is intended for unsafe but
67    /// perfect conversions. If the conversion can fail or is not perfect, use [`TryInto`].
68    ///
69    /// # Generic Implementations
70    ///
71    /// - [`UnsafeFrom`]`<T> for U` implies `UnsafeInto<U> for T`
72    /// - `UnsafeInto`, like [`Into`], is reflexive, which means that `UnsafeInto<T> for T` is
73    ///   implemented
74    /// - [`From`]`<T> for U` implies `UnsafeInto<U> for T`
75    /// - [`Into`]`<U> for T` implies `UnsafeInto<U> for T`
76    pub trait UnsafeInto<T> {
77        /// Unsafely converts this type into the input type.
78        unsafe fn unsafe_into(self) -> T;
79    }
80}