| 1 | |
| 2 |
|
| 3 | |
| 4 |
|
| 5 | pub mod projection;
|
| 6 | pub use crate::project_pointer as project;
|
| 7 |
|
| 8 | use core::mem::{
|
| 9 | align_of,
|
| 10 | size_of, |
| 11 | };
|
| 12 | use core::num::NonZero;
|
| 13 |
|
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | #[repr(transparent)]
|
| 28 | #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
| 29 | pub struct Alignment(NonZero<usize>);
|
| 30 |
|
| 31 | impl Alignment {
|
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | #[inline(always)]
|
| 46 | pub const fn new<const ALIGN: usize>() -> Self {
|
| 47 | const {
|
| 48 | assert!(
|
| 49 | ALIGN.is_power_of_two(),
|
| 50 | "Provided alignment is not a power of two."
|
| 51 | );
|
| 52 | }
|
| 53 |
|
| 54 | |
| 55 | |
| 56 | Self(unsafe { NonZero::new_unchecked(ALIGN) })
|
| 57 | }
|
| 58 |
|
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | #[inline(always)]
|
| 75 | pub const fn new_checked(align: usize) -> Option<Self> {
|
| 76 | if align.is_power_of_two() {
|
| 77 | |
| 78 | |
| 79 | Some(Self(unsafe { NonZero::new_unchecked(align) }))
|
| 80 | } else {
|
| 81 | None
|
| 82 | }
|
| 83 | }
|
| 84 |
|
| 85 | |
| 86 | |
| 87 | |
| 88 | #[inline(always)]
|
| 89 | pub const fn of<T>() -> Self {
|
| 90 | #![allow(clippy::incompatible_msrv)]
|
| 91 | |
| 92 | |
| 93 | |
| 94 | const { Alignment::new_checked(align_of::<T>()).unwrap() }
|
| 95 | }
|
| 96 |
|
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | #[inline(always)]
|
| 109 | pub const fn as_usize(self) -> usize {
|
| 110 | self.as_nonzero().get()
|
| 111 | }
|
| 112 |
|
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | #[inline(always)]
|
| 125 | pub const fn as_nonzero(self) -> NonZero<usize> {
|
| 126 | |
| 127 | |
| 128 | if !self.0.is_power_of_two() {
|
| 129 | |
| 130 | |
| 131 | unsafe { core::hint::unreachable_unchecked() }
|
| 132 | }
|
| 133 | self.0
|
| 134 | }
|
| 135 |
|
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | #[inline(always)]
|
| 147 | pub const fn log2(self) -> u32 {
|
| 148 | self.0.ilog2()
|
| 149 | }
|
| 150 |
|
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | #[inline(always)]
|
| 163 | pub const fn mask(self) -> usize {
|
| 164 | |
| 165 | |
| 166 | !(self.as_usize() - 1)
|
| 167 | }
|
| 168 | }
|
| 169 |
|
| 170 | |
| 171 | pub trait Alignable: Sized {
|
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | fn align_down(self, alignment: Alignment) -> Self;
|
| 184 |
|
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | fn align_up(self, alignment: Alignment) -> Option<Self>;
|
| 200 | }
|
| 201 |
|
| 202 | |
| 203 | macro_rules! impl_alignable_uint {
|
| 204 | ($($t:ty),*) => {
|
| 205 | $(
|
| 206 | impl Alignable for $t {
|
| 207 | #[inline(always)]
|
| 208 | fn align_down(self, alignment: Alignment) -> Self {
|
| 209 | |
| 210 | |
| 211 | ::core::num::NonZero::<Self>::try_from(alignment.as_nonzero())
|
| 212 | .map(|align| self & !(align.get() - 1))
|
| 213 | |
| 214 | .unwrap_or(0)
|
| 215 | }
|
| 216 |
|
| 217 | #[inline(always)]
|
| 218 | fn align_up(self, alignment: Alignment) -> Option<Self> {
|
| 219 | let aligned_down = self.align_down(alignment);
|
| 220 | if self == aligned_down {
|
| 221 | Some(aligned_down)
|
| 222 | } else {
|
| 223 | Self::try_from(alignment.as_usize())
|
| 224 | .ok()
|
| 225 | .and_then(|align| aligned_down.checked_add(align))
|
| 226 | }
|
| 227 | }
|
| 228 | }
|
| 229 | )*
|
| 230 | };
|
| 231 | }
|
| 232 |
|
| 233 | impl_alignable_uint!(u8, u16, u32, u64, usize);
|
| 234 |
|
| 235 | |
| 236 | |
| 237 | |
| 238 | pub trait KnownSize {
|
| 239 | |
| 240 | fn size(p: *const Self) -> usize;
|
| 241 | }
|
| 242 |
|
| 243 | impl<T> KnownSize for T {
|
| 244 | #[inline(always)]
|
| 245 | fn size(_: *const Self) -> usize {
|
| 246 | size_of::<T>()
|
| 247 | }
|
| 248 | }
|
| 249 |
|
| 250 | impl<T> KnownSize for [T] {
|
| 251 | #[inline(always)]
|
| 252 | fn size(p: *const Self) -> usize {
|
| 253 | p.len() * size_of::<T>()
|
| 254 | }
|
| 255 | }
|