diff --git a/.cargo/config.toml b/.cargo/config.toml deleted file mode 100644 index c1f0256..0000000 --- a/.cargo/config.toml +++ /dev/null @@ -1,2 +0,0 @@ -[alias] -reap-clippy = "reaper cargo clippy" diff --git a/.gitpod.Dockerfile b/.gitpod.Dockerfile deleted file mode 100644 index 557e63d..0000000 --- a/.gitpod.Dockerfile +++ /dev/null @@ -1,7 +0,0 @@ -FROM gitpod/workspace-full - -USER gitpod - -ENV DEBIAN_FRONTEND=noninteractive - -RUN cargo install cargo-reaper --git https://github.com/juntyr/grim-reaper -f diff --git a/.gitpod.yml b/.gitpod.yml deleted file mode 100644 index 192e2e0..0000000 --- a/.gitpod.yml +++ /dev/null @@ -1,21 +0,0 @@ -image: - file: .gitpod.Dockerfile - -vscode: - extensions: - - bungcip.better-toml - - serayuzgur.crates - - matklad.rust-analyzer - -github: - prebuilds: - # enable for the main/default branch (defaults to true) - master: true - # enable for all branches in this repo (defaults to false) - branches: true - # enable for pull requests coming from this repo (defaults to true) - pullRequests: true - # add a check to pull requests (defaults to true) - addCheck: true - # add a "Review in Gitpod" button as a comment to pull requests (defaults to false) - addComment: true diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index ed551a3..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "crates.localCargoIndexBranch": "origin/HEAD", - "rust-analyzer.procMacro.enable": false, - "rust-analyzer.updates.askBeforeDownload": false, - "rust-analyzer.checkOnSave.command": "reap-clippy", -} diff --git a/README.md b/README.md index b973d8c..8919a6e 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -[![CI Status]][workflow] [![MSRV]][repo] [![Latest Version]][crates.io] [![Rust Doc Crate]][docs.rs] [![Rust Doc Main]][docs] [![License Status]][fossa] [![Code Coverage]][codecov] [![Gitpod Ready-to-Code]][gitpod] +[![CI Status]][workflow] [![MSRV]][repo] [![Latest Version]][crates.io] [![Rust Doc Crate]][docs.rs] [![Rust Doc Main]][docs] [![License Status]][fossa] [![Code Coverage]][codecov] [CI Status]: https://img.shields.io/github/actions/workflow/status/juntyr/const-type-layout/ci.yml?branch=main [workflow]: https://github.com/juntyr/const-type-layout/actions/workflows/ci.yml?query=branch%3Amain @@ -21,9 +21,6 @@ [Code Coverage]: https://img.shields.io/codecov/c/github/juntyr/const-type-layout?token=J39WVBIMZX [codecov]: https://codecov.io/gh/juntyr/const-type-layout -[Gitpod Ready-to-Code]: https://img.shields.io/badge/Gitpod-ready-blue?logo=gitpod -[gitpod]: https://gitpod.io/#https://github.com/juntyr/const-type-layout - `const-type-layout` is a type layout comparison aid, providing a `#[derive]`able `TypeLayout` trait that reports: - The type's name, size, and minimum alignment diff --git a/const-type-layout-derive/src/lib.rs b/const-type-layout-derive/src/lib.rs index 30b561d..2a7b531 100644 --- a/const-type-layout-derive/src/lib.rs +++ b/const-type-layout-derive/src/lib.rs @@ -1,7 +1,6 @@ //! [![CI Status]][workflow] [![MSRV]][repo] [![Latest Version]][crates.io] //! [![Rust Doc Crate]][docs.rs] [![Rust Doc Main]][docs] //! [![License Status]][fossa] [![Code Coverage]][codecov] -//! [![Gitpod Ready-to-Code]][gitpod] //! //! [CI Status]: https://img.shields.io/github/actions/workflow/status/juntyr/const-type-layout/ci.yml?branch=main //! [workflow]: https://github.com/juntyr/const-type-layout/actions/workflows/ci.yml?query=branch%3Amain @@ -24,9 +23,6 @@ //! [Code Coverage]: https://img.shields.io/codecov/c/github/juntyr/const-type-layout?token=J39WVBIMZX //! [codecov]: https://codecov.io/gh/juntyr/const-type-layout //! -//! [Gitpod Ready-to-Code]: https://img.shields.io/badge/Gitpod-ready-blue?logo=gitpod -//! [gitpod]: https://gitpod.io/#https://github.com/juntyr/const-type-layout -//! //! `const-type-layout-derive` provides the [`#[derive(TypeLayout)`](TypeLayout) //! implementation for the //! [`const_type_layout::TypeLayout`](https://docs.rs/const-type-layout/0.3/const_type_layout/trait.TypeLayout.html) @@ -113,7 +109,7 @@ pub fn derive_type_layout(input: TokenStream) -> TokenStream { unsafe impl #type_layout_impl_generics #crate_path::TypeLayout for #ty_name #type_layout_ty_generics #type_layout_where_clause { - type Inhabited = #inhabited; + const INHABITED: #crate_path::MaybeUninhabited = #inhabited; const TYPE_LAYOUT: #crate_path::TypeLayoutInfo<'static> = { #crate_path::TypeLayoutInfo { @@ -554,11 +550,11 @@ fn quote_enum_variants( quote! { #crate_path::inhabited::all![#(#field_tys),*] } }, - syn::Fields::Unit => quote! { #crate_path::inhabited::Inhabited }, + syn::Fields::Unit => quote! { #crate_path::inhabited::all![] }, }; let discriminant = quote! { - #crate_path::MaybeUninhabited::new::<#variant_inhabited>( + #variant_inhabited.map( #crate_path::Discriminant::new::(#discriminant) ) }; @@ -663,7 +659,7 @@ fn inhabited_for_type(crate_path: &syn::Path, data: &syn::Data) -> proc_macro2:: | syn::Fields::Unnamed(syn::FieldsUnnamed { unnamed: fields, .. }) => fields, - syn::Fields::Unit => return quote! { #crate_path::inhabited::Inhabited }, + syn::Fields::Unit => return quote! { #crate_path::inhabited::all![] }, }; let field_tys = fields.iter().map(|syn::Field { ty, .. }| ty); @@ -678,7 +674,7 @@ fn inhabited_for_type(crate_path: &syn::Path, data: &syn::Data) -> proc_macro2:: | syn::Fields::Unnamed(syn::FieldsUnnamed { unnamed: fields, .. }) => fields, - syn::Fields::Unit => return quote! { #crate_path::inhabited::Inhabited }, + syn::Fields::Unit => return quote! { #crate_path::inhabited::all![] }, }; let field_tys = fields.iter().map(|syn::Field { ty, .. }| ty); @@ -687,9 +683,7 @@ fn inhabited_for_type(crate_path: &syn::Path, data: &syn::Data) -> proc_macro2:: }); // Enums are inhabited if they have at least one inhabited variant - quote! { - #crate_path::inhabited::any![#(#variants_inhabited),*] - } + quote! { #crate_path::inhabited::any![#({ #variants_inhabited }),*] } }, syn::Data::Union(syn::DataUnion { fields: syn::FieldsNamed { named: fields, .. }, diff --git a/src/impls/core/array.rs b/src/impls/core/array.rs index 159bb98..f29092d 100644 --- a/src/impls/core/array.rs +++ b/src/impls/core/array.rs @@ -4,8 +4,7 @@ use crate::{ }; unsafe impl TypeLayout for [T; N] { - type Inhabited = T::Inhabited; - + const INHABITED: crate::MaybeUninhabited = T::INHABITED; const TYPE_LAYOUT: TypeLayoutInfo<'static> = TypeLayoutInfo { name: ::core::any::type_name::(), size: ::core::mem::size_of::(), diff --git a/src/impls/core/cell.rs b/src/impls/core/cell.rs index e794425..cdf0ecf 100644 --- a/src/impls/core/cell.rs +++ b/src/impls/core/cell.rs @@ -4,8 +4,7 @@ use crate::{ }; unsafe impl TypeLayout for core::cell::UnsafeCell { - type Inhabited = T::Inhabited; - + const INHABITED: crate::MaybeUninhabited = T::INHABITED; const TYPE_LAYOUT: TypeLayoutInfo<'static> = TypeLayoutInfo { name: ::core::any::type_name::(), size: ::core::mem::size_of::(), @@ -26,8 +25,7 @@ unsafe impl ComputeTypeSet for core::cell::UnsafeCell { } unsafe impl TypeLayout for core::cell::Cell { - type Inhabited = T::Inhabited; - + const INHABITED: crate::MaybeUninhabited = T::INHABITED; const TYPE_LAYOUT: TypeLayoutInfo<'static> = TypeLayoutInfo { name: ::core::any::type_name::(), size: ::core::mem::size_of::(), @@ -48,8 +46,7 @@ unsafe impl ComputeTypeSet for core::cell::Cell { } unsafe impl TypeLayout for core::cell::SyncUnsafeCell { - type Inhabited = T::Inhabited; - + const INHABITED: crate::MaybeUninhabited = T::INHABITED; const TYPE_LAYOUT: TypeLayoutInfo<'static> = TypeLayoutInfo { name: ::core::any::type_name::(), size: ::core::mem::size_of::(), @@ -70,8 +67,7 @@ unsafe impl ComputeTypeSet for core::cell::SyncUnsafeCell } unsafe impl TypeLayout for core::cell::OnceCell { - type Inhabited = T::Inhabited; - + const INHABITED: crate::MaybeUninhabited = T::INHABITED; const TYPE_LAYOUT: TypeLayoutInfo<'static> = TypeLayoutInfo { name: ::core::any::type_name::(), size: ::core::mem::size_of::(), diff --git a/src/impls/core/cmp.rs b/src/impls/core/cmp.rs index fe77561..ed69637 100644 --- a/src/impls/core/cmp.rs +++ b/src/impls/core/cmp.rs @@ -4,8 +4,7 @@ use crate::{ }; unsafe impl TypeLayout for core::cmp::Reverse { - type Inhabited = T::Inhabited; - + const INHABITED: crate::MaybeUninhabited = T::INHABITED; const TYPE_LAYOUT: TypeLayoutInfo<'static> = TypeLayoutInfo { name: ::core::any::type_name::(), size: ::core::mem::size_of::(), @@ -26,8 +25,7 @@ unsafe impl ComputeTypeSet for core::cmp::Reverse { } unsafe impl TypeLayout for core::cmp::Ordering { - type Inhabited = crate::inhabited::Inhabited; - + const INHABITED: crate::MaybeUninhabited = crate::inhabited::all![]; const TYPE_LAYOUT: TypeLayoutInfo<'static> = TypeLayoutInfo { name: ::core::any::type_name::(), size: ::core::mem::size_of::(), diff --git a/src/impls/core/convert.rs b/src/impls/core/convert.rs index ea09117..1897d92 100644 --- a/src/impls/core/convert.rs +++ b/src/impls/core/convert.rs @@ -4,8 +4,7 @@ use crate::{ }; unsafe impl TypeLayout for core::convert::Infallible { - type Inhabited = crate::inhabited::Uninhabited; - + const INHABITED: crate::MaybeUninhabited = crate::inhabited::any![]; const TYPE_LAYOUT: TypeLayoutInfo<'static> = TypeLayoutInfo { name: ::core::any::type_name::(), size: ::core::mem::size_of::(), diff --git a/src/impls/core/ffi.rs b/src/impls/core/ffi.rs index 64cc3fa..1c09ddf 100644 --- a/src/impls/core/ffi.rs +++ b/src/impls/core/ffi.rs @@ -4,8 +4,7 @@ use crate::{ }; unsafe impl TypeLayout for core::ffi::c_void { - type Inhabited = crate::inhabited::Inhabited; - + const INHABITED: crate::MaybeUninhabited = crate::inhabited::all![]; const TYPE_LAYOUT: TypeLayoutInfo<'static> = TypeLayoutInfo { name: ::core::any::type_name::(), size: ::core::mem::size_of::(), diff --git a/src/impls/core/fn.rs b/src/impls/core/fn.rs index 2b91876..cf5890a 100644 --- a/src/impls/core/fn.rs +++ b/src/impls/core/fn.rs @@ -20,7 +20,7 @@ macro_rules! impl_fn_pointer_type_layout { }; (impl extern $abi:literal fn($($T:ident),*) -> $R:ident, $ty:ty, $demo:item) => { unsafe impl<$R: TypeLayout, $($T: TypeLayout),*> TypeLayout for $ty { - type Inhabited = crate::inhabited::Inhabited; + const INHABITED: crate::MaybeUninhabited = crate::inhabited::all![]; const TYPE_LAYOUT: TypeLayoutInfo<'static> = TypeLayoutInfo { name: ::core::any::type_name::(), @@ -63,7 +63,7 @@ macro_rules! impl_variadic_extern_fn_pointer_type_layout { unsafe impl<$R: TypeLayout, $($T: TypeLayout),*> TypeLayout for unsafe extern $abi fn($($T),*, ...) -> $R { - type Inhabited = crate::inhabited::Inhabited; + const INHABITED: crate::MaybeUninhabited = crate::inhabited::all![]; const TYPE_LAYOUT: TypeLayoutInfo<'static> = TypeLayoutInfo { name: ::core::any::type_name::(), diff --git a/src/impls/core/marker.rs b/src/impls/core/marker.rs index bb6e2dd..05b1bd9 100644 --- a/src/impls/core/marker.rs +++ b/src/impls/core/marker.rs @@ -4,8 +4,7 @@ use crate::{ }; unsafe impl TypeLayout for core::marker::PhantomData { - type Inhabited = crate::inhabited::Inhabited; - + const INHABITED: crate::MaybeUninhabited = crate::inhabited::all![]; const TYPE_LAYOUT: TypeLayoutInfo<'static> = TypeLayoutInfo { name: ::core::any::type_name::(), size: ::core::mem::size_of::(), @@ -22,8 +21,7 @@ unsafe impl ComputeTypeSet for core::marker::PhantomData { } unsafe impl TypeLayout for core::marker::PhantomPinned { - type Inhabited = crate::inhabited::Inhabited; - + const INHABITED: crate::MaybeUninhabited = crate::inhabited::all![]; const TYPE_LAYOUT: TypeLayoutInfo<'static> = TypeLayoutInfo { name: ::core::any::type_name::(), size: ::core::mem::size_of::(), diff --git a/src/impls/core/mem.rs b/src/impls/core/mem.rs index d738230..da41658 100644 --- a/src/impls/core/mem.rs +++ b/src/impls/core/mem.rs @@ -4,8 +4,7 @@ use crate::{ }; unsafe impl TypeLayout for core::mem::ManuallyDrop { - type Inhabited = T::Inhabited; - + const INHABITED: crate::MaybeUninhabited = T::INHABITED; const TYPE_LAYOUT: TypeLayoutInfo<'static> = TypeLayoutInfo { name: ::core::any::type_name::(), size: ::core::mem::size_of::(), @@ -26,8 +25,7 @@ unsafe impl ComputeTypeSet for core::mem::ManuallyDrop { } unsafe impl TypeLayout for core::mem::MaybeUninit { - type Inhabited = crate::inhabited::Inhabited; - + const INHABITED: crate::MaybeUninhabited = crate::inhabited::all![]; const TYPE_LAYOUT: TypeLayoutInfo<'static> = TypeLayoutInfo { name: ::core::any::type_name::(), size: ::core::mem::size_of::(), @@ -55,8 +53,7 @@ unsafe impl ComputeTypeSet for core::mem::MaybeUninit { } unsafe impl TypeLayout for core::mem::Discriminant { - type Inhabited = crate::inhabited::Inhabited; - + const INHABITED: crate::MaybeUninhabited = crate::inhabited::all![]; const TYPE_LAYOUT: TypeLayoutInfo<'static> = TypeLayoutInfo { name: ::core::any::type_name::(), size: ::core::mem::size_of::(), diff --git a/src/impls/core/num.rs b/src/impls/core/num.rs index 0a2c62f..b6ca5d1 100644 --- a/src/impls/core/num.rs +++ b/src/impls/core/num.rs @@ -6,7 +6,7 @@ use crate::{ macro_rules! impl_nonzero_type_layout { (impl $nz:ident => $ty:ty) => { unsafe impl TypeLayout for core::num::$nz { - type Inhabited = crate::inhabited::Inhabited; + const INHABITED: crate::MaybeUninhabited = crate::inhabited::all![]; const TYPE_LAYOUT: TypeLayoutInfo<'static> = TypeLayoutInfo { name: ::core::any::type_name::(), @@ -42,8 +42,7 @@ impl_nonzero_type_layout! { } unsafe impl TypeLayout for core::num::Wrapping { - type Inhabited = T::Inhabited; - + const INHABITED: crate::MaybeUninhabited = T::INHABITED; const TYPE_LAYOUT: TypeLayoutInfo<'static> = TypeLayoutInfo { name: ::core::any::type_name::(), size: ::core::mem::size_of::(), @@ -64,8 +63,7 @@ unsafe impl ComputeTypeSet for core::num::Wrapping { } unsafe impl TypeLayout for core::num::Saturating { - type Inhabited = T::Inhabited; - + const INHABITED: crate::MaybeUninhabited = T::INHABITED; const TYPE_LAYOUT: TypeLayoutInfo<'static> = TypeLayoutInfo { name: ::core::any::type_name::(), size: ::core::mem::size_of::(), diff --git a/src/impls/core/ops.rs b/src/impls/core/ops.rs index b9c8b12..c361139 100644 --- a/src/impls/core/ops.rs +++ b/src/impls/core/ops.rs @@ -4,8 +4,7 @@ use crate::{ }; unsafe impl TypeLayout for core::ops::Range { - type Inhabited = Idx::Inhabited; - + const INHABITED: crate::MaybeUninhabited = Idx::INHABITED; const TYPE_LAYOUT: TypeLayoutInfo<'static> = TypeLayoutInfo { name: ::core::any::type_name::(), size: ::core::mem::size_of::(), @@ -33,8 +32,7 @@ unsafe impl ComputeTypeSet for core::ops::Range { } unsafe impl TypeLayout for core::ops::RangeFrom { - type Inhabited = Idx::Inhabited; - + const INHABITED: crate::MaybeUninhabited = Idx::INHABITED; const TYPE_LAYOUT: TypeLayoutInfo<'static> = TypeLayoutInfo { name: ::core::any::type_name::(), size: ::core::mem::size_of::(), @@ -55,8 +53,7 @@ unsafe impl ComputeTypeSet for core::ops::RangeFrom { } unsafe impl TypeLayout for core::ops::RangeFull { - type Inhabited = crate::inhabited::Inhabited; - + const INHABITED: crate::MaybeUninhabited = crate::inhabited::all![]; const TYPE_LAYOUT: TypeLayoutInfo<'static> = TypeLayoutInfo { name: ::core::any::type_name::(), size: ::core::mem::size_of::(), @@ -73,8 +70,7 @@ unsafe impl ComputeTypeSet for core::ops::RangeFull { } unsafe impl TypeLayout for core::ops::RangeTo { - type Inhabited = Idx::Inhabited; - + const INHABITED: crate::MaybeUninhabited = Idx::INHABITED; const TYPE_LAYOUT: TypeLayoutInfo<'static> = TypeLayoutInfo { name: ::core::any::type_name::(), size: ::core::mem::size_of::(), @@ -95,8 +91,7 @@ unsafe impl ComputeTypeSet for core::ops::RangeTo { } unsafe impl TypeLayout for core::ops::RangeToInclusive { - type Inhabited = Idx::Inhabited; - + const INHABITED: crate::MaybeUninhabited = Idx::INHABITED; const TYPE_LAYOUT: TypeLayoutInfo<'static> = TypeLayoutInfo { name: ::core::any::type_name::(), size: ::core::mem::size_of::(), @@ -117,8 +112,7 @@ unsafe impl ComputeTypeSet for core::ops::RangeToInclusive< } unsafe impl TypeLayout for core::ops::Bound { - type Inhabited = crate::inhabited::Inhabited; - + const INHABITED: crate::MaybeUninhabited = crate::inhabited::all![]; const TYPE_LAYOUT: TypeLayoutInfo<'static> = TypeLayoutInfo { name: ::core::any::type_name::(), size: ::core::mem::size_of::(), @@ -165,8 +159,7 @@ unsafe impl ComputeTypeSet for core::ops::Bound { } unsafe impl TypeLayout for core::ops::ControlFlow { - type Inhabited = crate::inhabited::any![B, C]; - + const INHABITED: crate::MaybeUninhabited = crate::inhabited::any![B, C]; const TYPE_LAYOUT: TypeLayoutInfo<'static> = TypeLayoutInfo { name: ::core::any::type_name::(), size: ::core::mem::size_of::(), diff --git a/src/impls/core/option.rs b/src/impls/core/option.rs index 0d33823..87fa8cf 100644 --- a/src/impls/core/option.rs +++ b/src/impls/core/option.rs @@ -4,8 +4,7 @@ use crate::{ }; unsafe impl TypeLayout for core::option::Option { - type Inhabited = crate::inhabited::Inhabited; - + const INHABITED: crate::MaybeUninhabited = crate::inhabited::all![]; const TYPE_LAYOUT: TypeLayoutInfo<'static> = TypeLayoutInfo { name: ::core::any::type_name::(), size: ::core::mem::size_of::(), diff --git a/src/impls/core/pin.rs b/src/impls/core/pin.rs index cc7df2a..6d9ea6c 100644 --- a/src/impls/core/pin.rs +++ b/src/impls/core/pin.rs @@ -4,8 +4,7 @@ use crate::{ }; unsafe impl TypeLayout for core::pin::Pin { - type Inhabited = T::Inhabited; - + const INHABITED: crate::MaybeUninhabited = T::INHABITED; const TYPE_LAYOUT: TypeLayoutInfo<'static> = TypeLayoutInfo { name: ::core::any::type_name::(), size: ::core::mem::size_of::(), diff --git a/src/impls/core/primitive.rs b/src/impls/core/primitive.rs index 35ce7b4..7c99c7e 100644 --- a/src/impls/core/primitive.rs +++ b/src/impls/core/primitive.rs @@ -6,7 +6,7 @@ use crate::{ macro_rules! impl_primitive_type_layout { (impl $ty:ty => $val:expr) => { unsafe impl TypeLayout for $ty { - type Inhabited = crate::inhabited::Inhabited; + const INHABITED: crate::MaybeUninhabited = crate::inhabited::all![]; const TYPE_LAYOUT: TypeLayoutInfo<'static> = TypeLayoutInfo { name: ::core::any::type_name::(), @@ -33,8 +33,7 @@ impl_primitive_type_layout! { } unsafe impl TypeLayout for ! { - type Inhabited = crate::inhabited::Uninhabited; - + const INHABITED: crate::MaybeUninhabited = crate::inhabited::any![]; const TYPE_LAYOUT: TypeLayoutInfo<'static> = TypeLayoutInfo { name: ::core::any::type_name::(), size: ::core::mem::size_of::(), diff --git a/src/impls/core/ptr.rs b/src/impls/core/ptr.rs index d1a92c1..1bf5a79 100644 --- a/src/impls/core/ptr.rs +++ b/src/impls/core/ptr.rs @@ -4,8 +4,7 @@ use crate::{ }; unsafe impl TypeLayout for *const T { - type Inhabited = crate::inhabited::Inhabited; - + const INHABITED: crate::MaybeUninhabited = crate::inhabited::all![]; const TYPE_LAYOUT: TypeLayoutInfo<'static> = TypeLayoutInfo { name: ::core::any::type_name::(), size: ::core::mem::size_of::(), @@ -19,8 +18,7 @@ unsafe impl ComputeTypeSet for *const T { } unsafe impl TypeLayout for *mut T { - type Inhabited = crate::inhabited::Inhabited; - + const INHABITED: crate::MaybeUninhabited = crate::inhabited::all![]; const TYPE_LAYOUT: TypeLayoutInfo<'static> = TypeLayoutInfo { name: ::core::any::type_name::(), size: ::core::mem::size_of::(), @@ -34,8 +32,7 @@ unsafe impl ComputeTypeSet for *mut T { } unsafe impl TypeLayout for core::ptr::NonNull { - type Inhabited = crate::inhabited::Inhabited; - + const INHABITED: crate::MaybeUninhabited = crate::inhabited::all![]; const TYPE_LAYOUT: TypeLayoutInfo<'static> = TypeLayoutInfo { name: ::core::any::type_name::(), size: ::core::mem::size_of::(), diff --git a/src/impls/core/ref.rs b/src/impls/core/ref.rs index 135b3a1..8087752 100644 --- a/src/impls/core/ref.rs +++ b/src/impls/core/ref.rs @@ -4,8 +4,7 @@ use crate::{ }; unsafe impl<'a, T: TypeLayout + 'a> TypeLayout for &'a T { - type Inhabited = crate::inhabited::Inhabited; - + const INHABITED: crate::MaybeUninhabited = crate::inhabited::all![]; const TYPE_LAYOUT: TypeLayoutInfo<'static> = TypeLayoutInfo { name: ::core::any::type_name::(), size: ::core::mem::size_of::(), @@ -19,8 +18,7 @@ unsafe impl<'a, T: ComputeTypeSet + 'a> ComputeTypeSet for &'a T { } unsafe impl<'a, T: TypeLayout + 'a> TypeLayout for &'a mut T { - type Inhabited = crate::inhabited::Inhabited; - + const INHABITED: crate::MaybeUninhabited = crate::inhabited::all![]; const TYPE_LAYOUT: TypeLayoutInfo<'static> = TypeLayoutInfo { name: ::core::any::type_name::(), size: ::core::mem::size_of::(), diff --git a/src/impls/core/result.rs b/src/impls/core/result.rs index 54b275e..cdfc958 100644 --- a/src/impls/core/result.rs +++ b/src/impls/core/result.rs @@ -4,8 +4,7 @@ use crate::{ }; unsafe impl TypeLayout for core::result::Result { - type Inhabited = crate::inhabited::any![T, E]; - + const INHABITED: crate::MaybeUninhabited = crate::inhabited::any![T, E]; const TYPE_LAYOUT: TypeLayoutInfo<'static> = TypeLayoutInfo { name: ::core::any::type_name::(), size: ::core::mem::size_of::(), diff --git a/src/impls/core/sync/atomic.rs b/src/impls/core/sync/atomic.rs index 894cb15..8f2d681 100644 --- a/src/impls/core/sync/atomic.rs +++ b/src/impls/core/sync/atomic.rs @@ -7,7 +7,7 @@ macro_rules! impl_atomic_int_layout { (impl $at:ident ( $align:literal : $cfg:literal ) => $ty:ty => $val:literal) => { #[cfg(target_has_atomic_load_store = $cfg)] unsafe impl TypeLayout for core::sync::atomic::$at { - type Inhabited = crate::inhabited::Inhabited; + const INHABITED: crate::MaybeUninhabited = crate::inhabited::all![]; const TYPE_LAYOUT: TypeLayoutInfo<'static> = TypeLayoutInfo { name: ::core::any::type_name::(), @@ -50,7 +50,7 @@ macro_rules! impl_atomic_int_ptr_sized_layout { #[cfg(target_has_atomic_load_store = "ptr")] #[cfg(target_pointer_width = $cfg)] unsafe impl TypeLayout for core::sync::atomic::$at { - type Inhabited = crate::inhabited::Inhabited; + const INHABITED: crate::MaybeUninhabited = crate::inhabited::all![]; const TYPE_LAYOUT: TypeLayoutInfo<'static> = TypeLayoutInfo { name: ::core::any::type_name::(), @@ -92,7 +92,7 @@ macro_rules! impl_atomic_ptr_layout { #[cfg(target_has_atomic_load_store = "ptr")] #[cfg(target_pointer_width = $cfg)] unsafe impl TypeLayout for core::sync::atomic::AtomicPtr { - type Inhabited = crate::inhabited::Inhabited; + const INHABITED: crate::MaybeUninhabited = crate::inhabited::all![]; const TYPE_LAYOUT: TypeLayoutInfo<'static> = TypeLayoutInfo { name: ::core::any::type_name::(), diff --git a/src/impls/core/sync/mod.rs b/src/impls/core/sync/mod.rs index bba3177..e53a882 100644 --- a/src/impls/core/sync/mod.rs +++ b/src/impls/core/sync/mod.rs @@ -6,8 +6,7 @@ use crate::{ mod atomic; unsafe impl TypeLayout for core::sync::Exclusive { - type Inhabited = T::Inhabited; - + const INHABITED: crate::MaybeUninhabited = T::INHABITED; const TYPE_LAYOUT: TypeLayoutInfo<'static> = TypeLayoutInfo { name: ::core::any::type_name::(), size: ::core::mem::size_of::(), diff --git a/src/impls/core/tuple.rs b/src/impls/core/tuple.rs index 3885ce1..778280e 100644 --- a/src/impls/core/tuple.rs +++ b/src/impls/core/tuple.rs @@ -6,7 +6,7 @@ use crate::{ macro_rules! impl_tuple_type_layout { (impl ($($a:tt => $T:ident),+)) => { unsafe impl<$($T: TypeLayout),*> TypeLayout for ($($T,)*) { - type Inhabited = crate::inhabited::all![$($T),*]; + const INHABITED: crate::MaybeUninhabited = crate::inhabited::all![$($T),*]; const TYPE_LAYOUT: TypeLayoutInfo<'static> = TypeLayoutInfo { name: ::core::any::type_name::(), diff --git a/src/inhabited.rs b/src/inhabited.rs index ce372b9..1f2a28c 100644 --- a/src/inhabited.rs +++ b/src/inhabited.rs @@ -4,150 +4,72 @@ //! [inhabited]: https://doc.rust-lang.org/reference/glossary.html#inhabited //! [uninhabited]: https://doc.rust-lang.org/reference/glossary.html#uninhabited -#![allow(clippy::undocumented_unsafe_blocks)] - -/// Marker type used to specify that a type implementing -/// [`crate::TypeLayout::Inhabited`] is -/// [inhabited](https://doc.rust-lang.org/reference/glossary.html#inhabited). -pub struct Inhabited; - -unsafe impl crate::TypeLayout for Inhabited { - type Inhabited = Self; - - const TYPE_LAYOUT: crate::TypeLayoutInfo<'static> = crate::TypeLayoutInfo { - name: ::core::any::type_name::(), - size: ::core::mem::size_of::(), - alignment: ::core::mem::align_of::(), - structure: crate::TypeStructure::Struct { - repr: "", - fields: &[], - }, - }; -} - -unsafe impl crate::typeset::ComputeTypeSet for Inhabited { - type Output = crate::typeset::tset![.. @ T]; -} - -#[allow(clippy::empty_enum)] -/// Marker type used to specify that a type implementing -/// [`crate::TypeLayout::Inhabited`] is -/// [uninhabited](https://doc.rust-lang.org/reference/glossary.html#uninhabited). -pub enum Uninhabited {} - -unsafe impl crate::TypeLayout for Uninhabited { - type Inhabited = Self; - - const TYPE_LAYOUT: crate::TypeLayoutInfo<'static> = crate::TypeLayoutInfo { - name: ::core::any::type_name::(), - size: ::core::mem::size_of::(), - alignment: ::core::mem::align_of::(), - structure: crate::TypeStructure::Enum { - repr: "", - variants: &[], - }, - }; -} - -unsafe impl crate::typeset::ComputeTypeSet for Uninhabited { - type Output = crate::typeset::tset![.. @ T]; -} - -#[allow(clippy::module_name_repetitions)] -#[doc(hidden)] -pub trait ComputeInhabited: sealed::ComputeInhabited { - type Output: OutputMaybeInhabited; -} - -#[allow(clippy::module_name_repetitions)] -#[doc(hidden)] -pub trait OutputMaybeInhabited: - ComputeInhabited + crate::TypeGraphLayout + sealed::OutputMaybeInhabited -{ -} - -mod sealed { - pub trait ComputeInhabited {} - pub trait OutputMaybeInhabited {} -} - -impl sealed::ComputeInhabited for Inhabited {} -impl ComputeInhabited for Inhabited { - type Output = Self; -} -impl sealed::OutputMaybeInhabited for Inhabited {} -impl OutputMaybeInhabited for Inhabited {} - -impl sealed::ComputeInhabited for Uninhabited {} -impl ComputeInhabited for Uninhabited { - type Output = Self; -} -impl sealed::OutputMaybeInhabited for Uninhabited {} -impl OutputMaybeInhabited for Uninhabited {} - -mod logical { - use super::{sealed, ComputeInhabited, Inhabited, Uninhabited}; - - pub struct And { - _left: L, - _right: R, - } - - impl sealed::ComputeInhabited for And {} - impl ComputeInhabited for And { - default type Output = Uninhabited; - } - - impl, R: ComputeInhabited> - ComputeInhabited for And - { - type Output = Inhabited; - } - - pub struct Or { - _left: L, - _right: R, - } - - impl sealed::ComputeInhabited for Or {} - impl ComputeInhabited for Or { - default type Output = Inhabited; - } - - impl, R: ComputeInhabited> - ComputeInhabited for Or - { - type Output = Uninhabited; - } -} - -/// Helper macro to compute whether all of a list of types, all implementing -/// [`crate::TypeLayout`], e.g. `[T, U, V]`, are [inhabited]. +/// Helper macro to compute whether +/// +/// - all of a list of types, all implementing [`crate::TypeLayout`], e.g. `[T, +/// U, V]`, +/// - all of a list of braced expressions of type [`crate::MaybeUninhabited`], +/// e.g. `[{ all![] }, { any![] }, { all![] }]`, +/// +/// are [inhabited]. +/// +/// `all![]` resolves to [`crate::MaybeUninhabited::Inhabited`]. /// /// For instance, a struct is inhabited iff all of its fields are inhabited. /// The empty list of types is inhabited. This macro resolves into either -/// [`Inhabited`] or [`Uninhabited`]. +/// [`crate::MaybeUninhabited::Inhabited`] or +/// [`crate::MaybeUninhabited::Uninhabited`]. /// /// [inhabited]: https://doc.rust-lang.org/reference/glossary.html#inhabited -pub macro all { - () => { Inhabited }, +#[macro_export] +#[doc(hidden)] +macro_rules! all { + () => { $crate::MaybeUninhabited::Inhabited(()) }; ($L:ty $(, $R:ty)*) => { - ::Inhabited, all![$($R),*]> as ComputeInhabited>::Output - }, + <$L as $crate::TypeLayout>::INHABITED.and( + $crate::inhabited::all![$($R),*] + ) + }; + ({ $L:expr } $(, { $R:expr })*) => { + $crate::MaybeUninhabited::and( + $L, $crate::inhabited::all![$({ $R }),*] + ) + }; } -/// Helper macro to compute whether any of a list of types, all implementing -/// [`crate::TypeLayout`], e.g. `[T, U, V]`, is [inhabited]. +/// Helper macro to compute whether +/// +/// - any of a list of types, all implementing [`crate::TypeLayout`], e.g. `[T, +/// U, V]`, +/// - any of a list of braced expressions of type [`crate::MaybeUninhabited`], +/// e.g. `[{ all![] }, { any![] }, { all![] }]`, +/// +/// is [inhabited]. +/// +/// `any![]` resolves to [`crate::MaybeUninhabited::Uninhabited`]. /// /// For instance, an enum is inhabited iff any of its variants is inhabited. /// The empty list of types is [uninhabited]. This macro resolves into either -/// [`Inhabited`] or [`Uninhabited`]. +/// [`crate::MaybeUninhabited::Inhabited`] or +/// [`crate::MaybeUninhabited::Uninhabited`]. /// /// [inhabited]: https://doc.rust-lang.org/reference/glossary.html#inhabited /// [uninhabited]: https://doc.rust-lang.org/reference/glossary.html#uninhabited -pub macro any { - () => { Uninhabited }, +#[macro_export] +#[doc(hidden)] +macro_rules! any { + () => { $crate::MaybeUninhabited::Uninhabited }; ($L:ty $(, $R:ty)*) => { - ::Inhabited, any![$($R),*]> as ComputeInhabited>::Output - }, + <$L as $crate::TypeLayout>::INHABITED.or( + $crate::inhabited::any![$($R),*] + ) + }; + ({ $L:expr } $(, { $R:expr })*) => { + $crate::MaybeUninhabited::or( + $L, $crate::inhabited::any![$({ $R }),*] + ) + }; } + +#[doc(inline)] +pub use {all, any}; diff --git a/src/lib.rs b/src/lib.rs index 342f422..319107f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,6 @@ //! [![CI Status]][workflow] [![MSRV]][repo] [![Latest Version]][crates.io] //! [![Rust Doc Crate]][docs.rs] [![Rust Doc Main]][docs] //! [![License Status]][fossa] [![Code Coverage]][codecov] -//! [![Gitpod Ready-to-Code]][gitpod] //! //! [CI Status]: https://img.shields.io/github/actions/workflow/status/juntyr/const-type-layout/ci.yml?branch=main //! [workflow]: https://github.com/juntyr/const-type-layout/actions/workflows/ci.yml?query=branch%3Amain @@ -24,9 +23,6 @@ //! [Code Coverage]: https://img.shields.io/codecov/c/github/juntyr/const-type-layout?token=J39WVBIMZX //! [codecov]: https://codecov.io/gh/juntyr/const-type-layout //! -//! [Gitpod Ready-to-Code]: https://img.shields.io/badge/Gitpod-ready-blue?logo=gitpod -//! [gitpod]: https://gitpod.io/#https://github.com/juntyr/const-type-layout -//! //! `const-type-layout` is a type layout comparison aid, providing a //! [`#[derive]`](const_type_layout_derive::TypeLayout)able [`TypeLayout`] trait //! that provides a const [`TypeLayoutInfo`] struct containing: @@ -51,8 +47,6 @@ on non-`#[repr(C)]` types, but their layout is unpredictable. ```rust # #![feature(const_type_name)] -# #![feature(offset_of)] -# #![feature(offset_of_enum)] use const_type_layout::TypeLayout; #[derive(TypeLayout)] @@ -65,7 +59,7 @@ struct Foo { assert_eq!( format!("{:#?}", Foo::TYPE_LAYOUT), r#"TypeLayoutInfo { - name: "rust_out::main::_doctest_main_src_lib_rs_49_0::Foo", + name: "rust_out::main::_doctest_main_src_lib_rs_45_0::Foo", size: 8, alignment: 4, structure: Struct { @@ -96,8 +90,6 @@ FFI scenarios: ```rust # #![feature(const_type_name)] -# #![feature(offset_of)] -# #![feature(offset_of_enum)] use const_type_layout::TypeLayout; #[derive(TypeLayout)] @@ -109,7 +101,7 @@ struct OverAligned { assert_eq!( format!("{:#?}", OverAligned::TYPE_LAYOUT), r#"TypeLayoutInfo { - name: "rust_out::main::_doctest_main_src_lib_rs_94_0::OverAligned", + name: "rust_out::main::_doctest_main_src_lib_rs_88_0::OverAligned", size: 128, alignment: 128, structure: Struct { @@ -144,7 +136,7 @@ r#"TypeLayoutInfo { #![no_std] #![feature(const_type_name)] #![cfg_attr(not(version("1.83")), feature(const_mut_refs))] -#![feature(cfg_target_has_atomic)] +#![feature(cfg_target_has_atomic)] // https://github.com/rust-lang/rust/issues/94039 #![feature(decl_macro)] #![feature(never_type)] #![feature(discriminant_kind)] @@ -182,7 +174,7 @@ pub mod typeset; #[cfg_attr(feature = "serde", derive(::serde::Deserialize))] /// Optional value that exists if some other type is /// [inhabited](https://doc.rust-lang.org/reference/glossary.html#inhabited). -pub enum MaybeUninhabited { +pub enum MaybeUninhabited { /// The type is [uninhabited](https://doc.rust-lang.org/reference/glossary.html#uninhabited), /// no value. Uninhabited, @@ -194,14 +186,49 @@ pub enum MaybeUninhabited { impl MaybeUninhabited { #[must_use] /// Construct [`MaybeUninhabited::Inhabited`] iff [`::Inhabited`](TypeLayout::Inhabited) is - /// [`inhabited::Inhabited`], [`MaybeUninhabited::Uninhabited`] + /// TypeLayout>::INHABITED`][`TypeLayout::INHABITED`] is + /// [`MaybeUninhabited::Inhabited`], [`MaybeUninhabited::Uninhabited`] /// otherwise. pub const fn new(v: T) -> Self { - if >::EQ { - Self::Inhabited(v) - } else { - Self::Uninhabited + U::INHABITED.map(v) + } +} + +impl MaybeUninhabited { + #[must_use] + /// Maps a [`MaybeUninhabited::Inhabited`] to a + /// [`MaybeUninhabited::Inhabited`] with the value `v` or returns + /// [`MaybeUninhabited::Uninhabited`]. + pub const fn map(self, v: T) -> MaybeUninhabited { + match self { + Self::Inhabited(()) => MaybeUninhabited::Inhabited(v), + Self::Uninhabited => MaybeUninhabited::Uninhabited, + } + } + + #[must_use] + /// Returns the logical and between `self` and `other`. + /// + /// The result is [`MaybeUninhabited::Inhabited`] iff both `self` and + /// `other` are [`MaybeUninhabited::Inhabited`], + /// [`MaybeUninhabited::Uninhabited`] otherwise. + pub const fn and(self, other: Self) -> Self { + match (self, other) { + (Self::Inhabited(()), Self::Inhabited(())) => Self::Inhabited(()), + _ => Self::Uninhabited, + } + } + + #[must_use] + /// Returns the or and between `self` and `other`. + /// + /// The result is [`MaybeUninhabited::Uninhabited`] iff both `self` and + /// `other` are [`MaybeUninhabited::Uninhabited`], + /// [`MaybeUninhabited::Inhabited`] otherwise. + pub const fn or(self, b: Self) -> Self { + match (self, b) { + (Self::Uninhabited, Self::Uninhabited) => Self::Uninhabited, + _ => Self::Inhabited(()), } } } @@ -227,7 +254,6 @@ impl Default for MaybeUninhabited { /// /// ```rust /// # #![feature(const_type_name)] -/// # #![feature(offset_of)] /// # use const_type_layout::{ /// # Field, MaybeUninhabited, TypeLayout, TypeLayoutInfo, TypeStructure, /// # }; @@ -239,7 +265,7 @@ impl Default for MaybeUninhabited { /// } /// /// unsafe impl TypeLayout for Foo { -/// type Inhabited = inhabited::all![u8, u16]; +/// const INHABITED: MaybeUninhabited = inhabited::all![u8, u16]; /// /// const TYPE_LAYOUT: TypeLayoutInfo<'static> = TypeLayoutInfo { /// name: ::core::any::type_name::(), @@ -267,12 +293,10 @@ impl Default for MaybeUninhabited { /// Note that if you implement [`TypeLayout`], you should also implement /// [`typeset::ComputeTypeSet`] for it. pub unsafe trait TypeLayout: Sized { - /// Marker type for whether the type is + /// Marker for whether the type is /// [inhabited](https://doc.rust-lang.org/reference/glossary.html#inhabited) or /// [uninhabited](https://doc.rust-lang.org/reference/glossary.html#uninhabited). - /// The associated type must be either [`inhabited::Inhabited`] - /// or [`inhabited::Uninhabited`]. - type Inhabited: inhabited::OutputMaybeInhabited; + const INHABITED: MaybeUninhabited; /// Shallow layout of the type. const TYPE_LAYOUT: TypeLayoutInfo<'static>; diff --git a/src/typeset.rs b/src/typeset.rs index 8c8bdb8..5c4b9e6 100644 --- a/src/typeset.rs +++ b/src/typeset.rs @@ -46,7 +46,7 @@ type Set = ::Output; /// } /// /// # unsafe impl TypeLayout for Foo { -/// # type Inhabited = inhabited::all![u8, u16]; +/// # const INHABITED: MaybeUninhabited = inhabited::all![u8, u16]; /// # /// # const TYPE_LAYOUT: TypeLayoutInfo<'static> = TypeLayoutInfo { /// # name: ::core::any::type_name::(),