Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ categories = ["embedded", "no-std", "data-structures"]
[dependencies]

[features]
default = []
default = ["const-generics"]
# This is a no-op feature only provided for compatibility in case it is
# explicitly selected by a user. This crate works without explicit indication
# both on std and no_std systems.
std = []
const-generics = []
61 changes: 61 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,61 @@ use lib::core::cmp::{Ord, Ordering, PartialOrd};

use lib::core::fmt::{Binary, Display, Formatter, LowerHex, Octal, UpperHex};

#[cfg(feature = "const-generics")]
pub trait ToUnsignedType {
type Output;
}

#[cfg(feature = "const-generics")]
pub trait ToSignedType {
type Output;
}

#[cfg(feature = "const-generics")]
pub struct Const<const N: usize>;

#[cfg(feature = "const-generics")]
pub type Unsigned<const N: usize> = <Const<N> as ToUnsignedType>::Output;

#[cfg(feature = "const-generics")]
pub type Signed<const N: usize> = <Const<N> as ToSignedType>::Output;

#[cfg(feature = "const-generics")]
macro_rules! impl_type_dispatch {
(signed, $name:ident, $bits:expr) => {
impl ToSignedType for Const<$bits> {
type Output = $name;
}
};
(unsigned, $name:ident, $bits:expr) => {
impl ToUnsignedType for Const<$bits> {
type Output = $name;
}
};
}

#[cfg(feature = "const-generics")]
impl_type_dispatch!(signed, i8, 8);
#[cfg(feature = "const-generics")]
impl_type_dispatch!(signed, i16, 16);
#[cfg(feature = "const-generics")]
impl_type_dispatch!(signed, i32, 32);
#[cfg(feature = "const-generics")]
impl_type_dispatch!(signed, i64, 64);
#[cfg(feature = "const-generics")]
impl_type_dispatch!(signed, i128, 128);

#[cfg(feature = "const-generics")]
impl_type_dispatch!(unsigned, u8, 8);
#[cfg(feature = "const-generics")]
impl_type_dispatch!(unsigned, u16, 16);
#[cfg(feature = "const-generics")]
impl_type_dispatch!(unsigned, u32, 32);
#[cfg(feature = "const-generics")]
impl_type_dispatch!(unsigned, u64, 64);
#[cfg(feature = "const-generics")]
impl_type_dispatch!(unsigned, u128, 128);

macro_rules! define_unsigned {
($name:ident, $bits:expr, $type:ident) => {define_unsigned!(#[doc=""], $name, $bits, $type);};
(#[$doc:meta], $name:ident, $bits:expr, $type:ident) => {
Expand All @@ -51,6 +106,9 @@ macro_rules! define_unsigned {

implement_common!($name, $bits, $type);

#[cfg(feature = "const-generics")]
impl_type_dispatch!(unsigned, $name, $bits);

}
}

Expand Down Expand Up @@ -79,6 +137,9 @@ macro_rules! define_signed {

implement_common!($name, $bits, $type);

#[cfg(feature = "const-generics")]
impl_type_dispatch!(signed, $name, $bits);

}
}

Expand Down