proc_bitfield/
conv.rs

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