cuneiform_fields/
arch.rs

1//!
2//! Architecture system uses default Rust supported platforms and architectures.
3//!
4//! Alignment values are not randomly chosen or incorporated directly.
5//! Values are considered and incorporated inside with the mindset of preventing false sharing
6//! or creating less warp points in exclusive caching.
7//!
8//! Please head to the documentation of [Cuneiform](https://docs.rs/cuneiform/0.1.0/src/cuneiform/slabs.rs.html#5) for more info.
9
10use core::fmt;
11use core::ops::{Deref, DerefMut};
12use cuneiform::cuneiform;
13
14///
15/// Applies padding based on the architecture detected by cuneiform.
16/// This struct allows us to have padding on only wrapped field.
17///
18/// Cache line size is determined by a few steps:
19/// * It checks the current compiler architecture is already known by cuneiform.
20///     * If known it applies that value as alignment for the field.
21/// * If it is still not detected then fallback padding will be used.
22///
23#[cuneiform(hermetic = false)]
24#[derive(Clone, Copy, Default, Hash, PartialEq, Eq)]
25pub struct ArchPadding<T> {
26    value: T,
27}
28
29unsafe impl<T: Send> Send for ArchPadding<T> {}
30unsafe impl<T: Sync> Sync for ArchPadding<T> {}
31
32impl<T> ArchPadding<T> {
33    /// Applies padding based on the architecture detected by cuneiform.
34    ///
35    /// # Examples
36    ///
37    /// ```
38    /// use cuneiform_fields::arch::ArchPadding;
39    ///
40    /// let padded = ArchPadding::new(1);
41    /// ```
42    pub fn new(t: T) -> ArchPadding<T> {
43        ArchPadding::<T> { value: t }
44    }
45
46    /// Returns the inner value.
47    ///
48    /// # Examples
49    ///
50    /// ```
51    /// use cuneiform_fields::arch::ArchPadding;
52    ///
53    /// let padded = ArchPadding::new(7);
54    /// let value = padded.into_inner();
55    /// assert_eq!(value, 7);
56    /// ```
57    pub fn into_inner(self) -> T {
58        self.value
59    }
60}
61
62impl<T> Deref for ArchPadding<T> {
63    type Target = T;
64
65    fn deref(&self) -> &T {
66        &self.value
67    }
68}
69
70impl<T> DerefMut for ArchPadding<T> {
71    fn deref_mut(&mut self) -> &mut T {
72        &mut self.value
73    }
74}
75
76impl<T: fmt::Debug> fmt::Debug for ArchPadding<T> {
77    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
78        f.debug_struct("ArchPadding")
79            .field("value", &self.value)
80            .finish()
81    }
82}
83
84impl<T> From<T> for ArchPadding<T> {
85    fn from(t: T) -> Self {
86        ArchPadding::new(t)
87    }
88}