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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// devela::mem::trait
//
//! Functionality related to byte sizes.
//

#[cfg(all(not(feature = "safe_mem"), feature = "unsafe_slice"))]
use super::{mem_as_bytes, mem_as_bytes_mut};
use super::{mem_drop, mem_forget, mem_needs_drop, mem_replace, mem_swap, mem_take};

impl<T: ?Sized> Mem for T {}

/// A trait for type memory information and manipulation.
///
/// This trait is automatically implemented for every `?Sized` type,
/// although most methods are only available where `Self: Sized`.
pub trait Mem {
    /// Whether dropping values of this type matters.
    const NEEDS_DROP: bool = mem_needs_drop::<Self>();

    /// Returns `true` if dropping values of this type matters.
    fn mem_needs_drop(&self) -> bool {
        Self::NEEDS_DROP
    }

    /// Drops `self` by running its destructor.
    fn mem_drop(self)
    where
        Self: Sized,
    {
        mem_drop(self)
    }

    /// Forgets about `self` *without running its destructor*.
    fn mem_forget(self)
    where
        Self: Sized,
    {
        mem_forget(self)
    }

    /// Replaces `self` with other, returning the previous value of `self`.
    fn mem_replace(&mut self, other: Self) -> Self
    where
        Self: Sized,
    {
        mem_replace(self, other)
    }

    /// Replaces `self` with its default value, returning the previous value of `self`.
    fn mem_take(&mut self) -> Self
    where
        Self: Default,
    {
        mem_take(self)
    }

    /// Swaps the value of `self` and `other` without deinitializing either one.
    fn mem_swap(&mut self, other: &mut Self)
    where
        Self: Sized,
    {
        mem_swap(self, other);
    }

    /// View a `Sync + Unpin` `self` as `&[u8]`.
    ///
    /// For the `const` version for sized types see [`mem_as_bytes_sized`].
    #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "unsafe_slice")))]
    #[cfg(all(not(feature = "safe_mem"), feature = "unsafe_slice"))]
    fn mem_as_bytes(&self) -> &[u8]
    where
        Self: Sync + Unpin,
    {
        mem_as_bytes(self)
    }

    /// View a `Sync + Unpin` `self` as `&mut [u8]`.
    #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "unsafe_slice")))]
    #[cfg(all(not(feature = "safe_mem"), feature = "unsafe_slice"))]
    fn mem_as_bytes_mut(&mut self) -> &mut [u8]
    where
        Self: Sync + Unpin,
    {
        mem_as_bytes_mut(self)
    }
}