intuicio_data/
lib.rs

1pub mod data_stack;
2pub mod lifetime;
3pub mod managed;
4pub mod managed_box;
5pub mod shared;
6pub mod type_hash;
7
8pub mod prelude {
9    pub use crate::{
10        data_stack::*, lifetime::*, managed::*, managed_box::*, shared::*, type_hash::*, Finalize,
11        Initialize,
12    };
13}
14
15pub trait Initialize: Sized {
16    fn initialize() -> Self;
17
18    /// # Safety
19    unsafe fn initialize_raw(data: *mut ()) {
20        data.cast::<Self>().write(Self::initialize());
21    }
22}
23
24impl<T> Initialize for T
25where
26    T: Default,
27{
28    fn initialize() -> Self {
29        Self::default()
30    }
31}
32
33pub trait Finalize: Sized {
34    /// # Safety
35    unsafe fn finalize_raw(data: *mut ()) {
36        data.cast::<Self>().read_unaligned();
37    }
38}
39
40impl<T> Finalize for T {}
41
42#[inline]
43pub fn pointer_alignment_padding(pointer: *const u8, alignment: usize) -> usize {
44    let mut result = (pointer as usize) % alignment;
45    if result > 0 {
46        result = alignment - result;
47    }
48    result
49}
50
51#[macro_export]
52macro_rules! does_implement_trait {
53    ($trait:path => $identifier:ident < $type:ident >) => {
54        pub fn $identifier<$type>() -> bool {
55            struct ImplementsTrait<'a, $type> {
56                implements: &'a std::cell::Cell<bool>,
57                _marker: std::marker::PhantomData<$type>,
58            }
59
60            #[allow(clippy::non_canonical_clone_impl)]
61            impl<$type> Clone for ImplementsTrait<'_, $type> {
62                fn clone(&self) -> Self {
63                    self.implements.set(false);
64                    ImplementsTrait {
65                        implements: self.implements,
66                        _marker: std::marker::PhantomData,
67                    }
68                }
69            }
70
71            impl<$type: $trait> Copy for ImplementsTrait<'_, $type> {}
72
73            let implements = std::cell::Cell::new(true);
74            let _ = [ImplementsTrait::<$type> {
75                implements: &implements,
76                _marker: std::marker::PhantomData,
77            }]
78            .clone();
79            implements.get()
80        }
81    };
82}
83
84does_implement_trait!(Send => is_send<T>);
85does_implement_trait!(Sync => is_sync<T>);
86does_implement_trait!(Copy => is_copy<T>);
87does_implement_trait!(Clone => is_clone<T>);
88does_implement_trait!(Sized => is_sized<T>);
89does_implement_trait!(Unpin => is_unpin<T>);
90does_implement_trait!(ToString => is_to_string<T>);
91
92#[cfg(test)]
93mod tests {
94    use super::*;
95    use std::{marker::PhantomPinned, rc::Rc};
96
97    struct Foo;
98
99    #[test]
100    fn test_does_implement_trait() {
101        assert!(is_send::<i32>());
102        assert!(!is_send::<Rc<i32>>());
103
104        assert!(is_sync::<i32>());
105        assert!(!is_sync::<Rc<i32>>());
106
107        assert!(is_copy::<i32>());
108        assert!(!is_copy::<Foo>());
109
110        assert!(is_clone::<i32>());
111        assert!(!is_clone::<Foo>());
112
113        assert!(is_sized::<[i32; 1]>());
114
115        assert!(is_unpin::<&i32>());
116        assert!(!is_unpin::<PhantomPinned>());
117
118        assert!(is_to_string::<i32>());
119        assert!(!is_to_string::<Foo>());
120    }
121}