Skip to content

Commit

Permalink
add bytemuck::NoUninit trait to all Id types
Browse files Browse the repository at this point in the history
  • Loading branch information
aiju committed Oct 29, 2024
1 parent 4df22e7 commit cb9e0ec
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 29 deletions.
22 changes: 18 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions derive/src/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub fn derive_id(input: DeriveInput, internal_generic_id: bool) -> syn::Result<T

let Id = quote![#imctk_ids::Id];
let ConstIdFromIdIndex = quote![#imctk_ids::ConstIdFromIdIndex];
let NoUninit = quote![#imctk_ids::NoUninit];

let Clone = quote![::core::prelude::rust_2021::Clone];
let Copy = quote![::core::prelude::rust_2021::Copy];
Expand Down Expand Up @@ -113,6 +114,10 @@ pub fn derive_id(input: DeriveInput, internal_generic_id: bool) -> syn::Result<T
// SAFETY: we ensure that the newtype wrapped type is `Id` and thus `Sync`
unsafe impl #impl_generics #Sync for #target_type #where_clause {
}

// SAFETY: we ensure that the newtype wrapped type is `Id` and thus `NoUninit`
unsafe impl #impl_generics #NoUninit for #target_type #where_clause {
}
}
};

Expand Down
1 change: 1 addition & 0 deletions ids/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ imctk-derive = { path = "../derive" }
imctk-transparent = { path = "../transparent" }
table_seq = { path = "../table_seq" }
zwohash = "0.1.2"
bytemuck = { version = "1.19.0", features = ["derive"] }

[dev-dependencies]
rand = { version = "0.8.5", features = ["small_rng"] }
9 changes: 8 additions & 1 deletion ids/src/id.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::NoUninit;
use core::{fmt::Debug, hash::Hash};

mod id_types;
Expand Down Expand Up @@ -43,7 +44,7 @@ pub trait ConstIdFromIdIndex<const INDEX: usize> {
///
/// Users of this trait may depend on implementing types following these requirements for upholding
/// their own safety invariants.
pub unsafe trait Id: Copy + Ord + Hash + Send + Sync + Debug {
pub unsafe trait Id: Copy + Ord + Hash + Send + Sync + Debug + NoUninit {
/// An [`Id`] type that has the same representation and index range as this type.
///
/// This is provided to enable writing generic code that during monomorphization is only
Expand Down Expand Up @@ -161,6 +162,12 @@ pub unsafe trait Id: Copy + Ord + Hash + Send + Sync + Debug {
#[repr(transparent)]
pub struct GenericId<const MAX_ID_INDEX: usize, Repr: Id = usize>(Repr);

// SAFETY: #[repr(transparent)] and the only field is explicitly required to be NoUninit
unsafe impl<const MAX_ID_INDEX: usize, Repr> NoUninit for GenericId<MAX_ID_INDEX, Repr> where
Repr: Id + NoUninit
{
}

impl<const MAX_ID_INDEX: usize, Repr: Id> Debug for GenericId<MAX_ID_INDEX, Repr> {
#[inline(always)]
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Expand Down
28 changes: 14 additions & 14 deletions ids/src/id/id_types.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
mod id8 {
use imctk_transparent::SubtypeCast;

use crate::id::{u8_range_types::NonMaxHighNibbleU8, ConstIdFromIdIndex, GenericId, Id};
use crate::id::{u8_range_types::NonMaxHighNibbleU8, ConstIdFromIdIndex, GenericId, Id, NoUninit};
use core::{fmt, fmt::Debug, hash::Hash};

/// [`Id`] type representing indices in the range `0..0xf0`.
#[allow(dead_code)] // Only constructed via transmutation and/or pointer casts
#[derive(Clone, Copy)]
#[derive(Clone, Copy, NoUninit)]
#[repr(transparent)]
pub struct Id8(NonMaxHighNibbleU8);

Expand Down Expand Up @@ -177,13 +177,13 @@ mod id8 {
mod id16 {
use imctk_transparent::SubtypeCast;

use crate::id::{u8_range_types::NonMaxU8, ConstIdFromIdIndex, GenericId, Id};
use crate::id::{u8_range_types::NonMaxU8, ConstIdFromIdIndex, GenericId, Id, NoUninit};
use core::{fmt, fmt::Debug, hash::Hash};

/// [`Id`] type representing indices in the range `0..0xff00`.
#[cfg(target_endian = "little")]
#[allow(dead_code)] // Only constructed via transmutation and/or pointer casts
#[derive(Clone, Copy)]
#[derive(Clone, Copy, NoUninit)]
#[repr(C, align(2))]
pub struct Id16 {
lsb: u8,
Expand All @@ -192,7 +192,7 @@ mod id16 {

#[cfg(target_endian = "big")]
#[allow(dead_code)] // Only constructed via transmutation and/or pointer casts
#[derive(Clone, Copy)]
#[derive(Clone, Copy, NoUninit)]
#[repr(C, align(2))]
pub struct Id16 {
msb: NonMaxU8,
Expand Down Expand Up @@ -366,13 +366,13 @@ mod id16 {
mod id32 {
use imctk_transparent::SubtypeCast;

use crate::id::{u8_range_types::NonMaxU8, ConstIdFromIdIndex, GenericId, Id};
use crate::id::{u8_range_types::NonMaxU8, ConstIdFromIdIndex, GenericId, Id, NoUninit};
use core::{fmt, fmt::Debug, hash::Hash};

/// [`Id`] type representing indices in the range `0..0xff00_0000`.
#[cfg(target_endian = "little")]
#[allow(dead_code)] // Only constructed via transmutation and/or pointer casts
#[derive(Clone, Copy)]
#[derive(Clone, Copy, NoUninit)]
#[repr(C, align(4))]
pub struct Id32 {
lsbs: [u8; 3],
Expand All @@ -381,7 +381,7 @@ mod id32 {

#[cfg(target_endian = "big")]
#[allow(dead_code)] // Only constructed via transmutation and/or pointer casts
#[derive(Clone, Copy)]
#[derive(Clone, Copy, NoUninit)]
#[repr(C, align(4))]
pub struct Id32 {
msb: NonMaxU8,
Expand Down Expand Up @@ -562,13 +562,13 @@ mod id32 {
mod id64 {
use imctk_transparent::SubtypeCast;

use crate::id::{u8_range_types::NonMaxU8, ConstIdFromIdIndex, GenericId, Id};
use crate::id::{u8_range_types::NonMaxU8, ConstIdFromIdIndex, GenericId, Id, NoUninit};
use core::{fmt, fmt::Debug, hash::Hash};

/// [`Id`] type representing indices in the range `0..0xff00_0000_0000_0000`.
#[cfg(target_endian = "little")]
#[allow(dead_code)] // Only constructed via transmutation and/or pointer casts
#[derive(Clone, Copy)]
#[derive(Clone, Copy, NoUninit)]
#[repr(C, align(8))]
pub struct Id64 {
lsbs: [u8; 7],
Expand All @@ -577,7 +577,7 @@ mod id64 {

#[cfg(target_endian = "big")]
#[allow(dead_code)] // Only constructed via transmutation and/or pointer casts
#[derive(Clone, Copy)]
#[derive(Clone, Copy, NoUninit)]
#[repr(C, align(8))]
pub struct Id64 {
msb: NonMaxU8,
Expand Down Expand Up @@ -758,15 +758,15 @@ mod id64 {
mod id_size {
use imctk_transparent::SubtypeCast;

use crate::id::{u8_range_types::NonMaxMsbU8, ConstIdFromIdIndex, GenericId, Id};
use crate::id::{u8_range_types::NonMaxMsbU8, ConstIdFromIdIndex, GenericId, Id, NoUninit};
use core::{fmt, fmt::Debug, hash::Hash};

const LSBS: usize = (usize::BITS as usize / 8) - 1;

/// [`Id`] type representing indices in the range `0..=isize::MAX as usize`.
#[cfg(target_endian = "little")]
#[allow(dead_code)] // Only constructed via transmutation and/or pointer casts
#[derive(Clone, Copy)]
#[derive(Clone, Copy, NoUninit)]
#[cfg_attr(target_pointer_width = "16", repr(C, align(2)))]
#[cfg_attr(target_pointer_width = "32", repr(C, align(4)))]
#[cfg_attr(target_pointer_width = "64", repr(C, align(8)))]
Expand All @@ -777,7 +777,7 @@ mod id_size {

#[cfg(target_endian = "big")]
#[allow(dead_code)] // Only constructed via transmutation and/or pointer casts
#[derive(Clone, Copy)]
#[derive(Clone, Copy, NoUninit)]
#[cfg_attr(target_pointer_width = "16", repr(C, align(2)))]
#[cfg_attr(target_pointer_width = "32", repr(C, align(4)))]
#[cfg_attr(target_pointer_width = "64", repr(C, align(8)))]
Expand Down
8 changes: 5 additions & 3 deletions ids/src/id/u8_range_types.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::NoUninit;

#[allow(dead_code)] // Only constructed via transmutation and/or pointer casts
#[derive(Clone, Copy)]
#[derive(Clone, Copy, NoUninit)]
#[repr(u8)]
pub enum NonMaxU8 {
Val00 = 0x00,
Expand Down Expand Up @@ -260,7 +262,7 @@ pub enum NonMaxU8 {
}

#[allow(dead_code)] // Only constructed via transmutation and/or pointer casts
#[derive(Clone, Copy)]
#[derive(Clone, Copy, NoUninit)]
#[repr(u8)]
pub enum NonMaxHighNibbleU8 {
Val00 = 0x00,
Expand Down Expand Up @@ -506,7 +508,7 @@ pub enum NonMaxHighNibbleU8 {
}

#[allow(dead_code)] // Only constructed via transmutation and/or pointer casts
#[derive(Clone, Copy)]
#[derive(Clone, Copy, NoUninit)]
#[repr(u8)]
pub enum NonMaxMsbU8 {
Val00 = 0x00,
Expand Down
4 changes: 4 additions & 0 deletions ids/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@ pub use imctk_derive::Id;
pub use id::{ConstIdFromIdIndex, GenericId, Id, Id16, Id32, Id64, Id8, IdSize};

pub use id_range::IdRange;

// re-export this so that others can use it without depending on bytemuck explicitly
// in particular needed for #[derive(Id)]
pub use bytemuck::NoUninit;
2 changes: 1 addition & 1 deletion ids/tests/test_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ fn conversion_usize() {

#[derive(Id)]
#[repr(transparent)]
pub struct NewtypePhantom<T>(usize, PhantomData<T>);
pub struct NewtypePhantom<T: 'static>(usize, PhantomData<T>);

impl<T> std::fmt::Debug for NewtypePhantom<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Expand Down
3 changes: 1 addition & 2 deletions lit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,4 @@ imctk-transparent = { version = "0.1.0", path = "../transparent" }
imctk-util = { version = "0.1.0", path = "../util" }
log = "0.4.21"
table_seq = { version = "0.1.0", path = "../table_seq" }
zwohash = "0.1.2"
bytemuck = "*"
zwohash = "0.1.2"
2 changes: 0 additions & 2 deletions lit/src/lit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ use super::{pol::Pol, var::Var};
#[derive(Id, SubtypeCast, NewtypeCast)]
pub struct Lit(Id32);

unsafe impl bytemuck::NoUninit for Lit {}

/// Ensure that there is an even number of literals
#[allow(clippy::assertions_on_constants)]
const _: () = {
Expand Down
2 changes: 0 additions & 2 deletions lit/src/var.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ use super::{lit::Lit, pol::Pol};
#[derive(Id, SubtypeCast, NewtypeCast)]
pub struct Var(GenericId<{ Lit::MAX_ID_INDEX / 2 }, <Lit as Id>::BaseId>);

unsafe impl bytemuck::NoUninit for Var {}

impl std::fmt::Debug for Var {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(self, f)
Expand Down

0 comments on commit cb9e0ec

Please sign in to comment.