pub use sealed::Length;
#[derive(Debug, Clone, Copy)]
pub struct Dynamic(pub usize);
impl From<usize> for Dynamic {
fn from(value: usize) -> Dynamic {
Dynamic(value)
}
}
#[derive(Debug, Clone, Copy)]
pub struct Static<const N: usize>;
unsafe impl Length for Dynamic {
#[inline(always)]
fn value(self) -> usize {
self.0
}
}
unsafe impl<const N: usize> Length for Static<N> {
#[inline(always)]
fn value(self) -> usize {
N
}
}
mod sealed {
pub unsafe trait Length: Copy {
fn value(self) -> usize;
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_length() {
for i in 0..100 {
assert_eq!(Dynamic(i).value(), i);
}
assert_eq!(Static::<0>.value(), 0);
assert_eq!(Static::<10>.value(), 10);
assert_eq!(Static::<20>.value(), 20);
assert_eq!(Static::<37>.value(), 37);
}
}