Skip to content

Commit 9a0664e

Browse files
Merge pull request #8 from rodrimati1992/more_const_markers
1.6.0 release
2 parents 6a1b8b0 + dc5405f commit 9a0664e

File tree

15 files changed

+833
-22
lines changed

15 files changed

+833
-22
lines changed

.github/workflows/rust.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ jobs:
5858
cargo test --no-default-features --features "rust_stable nightly_mut_refs"
5959
cargo test --no-default-features --features "rust_stable alloc nightly_mut_refs"
6060
cargo test --no-default-features --features \
61-
"rust_stable const_marker __ui_tests alloc nightly_mut_refs"
61+
"rust_stable adt_const_marker __ui_tests alloc nightly_mut_refs"
6262
6363
MIRI_NIGHTLY=nightly-$(curl -s https://rust-lang.github.io/rustup-components-history/x86_64-unknown-linux-gnu/miri)
6464
echo "Installing latest nightly with Miri"
@@ -72,4 +72,4 @@ jobs:
7272
cargo clean
7373
7474
env RUST_BACKTRACE=0 cargo miri test --no-default-features \
75-
--features "${{env.rustv}} const_marker alloc nightly_mut_refs"
75+
--features "${{env.rustv}} adt_const_marker alloc nightly_mut_refs"

Cargo.toml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "typewit"
3-
version = "1.5.0"
3+
version = "1.6.0"
44
authors = ["rodrimati1992 <rodrimatt1985@gmail.com>"]
55
rust-version = "1.57.0"
66
edition = "2021"
@@ -18,6 +18,10 @@ include = [
1818
"LICENSE-ZLIB.md",
1919
]
2020

21+
[dev-dependencies.arrayvec]
22+
version = "0.7"
23+
default-features = false
24+
2125
[dev-dependencies.trybuild]
2226
version = "1.0"
2327

@@ -26,9 +30,10 @@ default = ["const_marker"]
2630
rust_1_61 = []
2731
rust_stable = ["rust_1_61"]
2832
const_marker = []
33+
adt_const_marker = ["rust_stable","const_marker"]
2934
alloc = []
3035
mut_refs = []
31-
nightly_mut_refs = ["mut_refs"]
36+
nightly_mut_refs = ["rust_stable", "mut_refs"]
3237
docsrs = []
3338
__ui_tests = []
3439

Changelog.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,34 @@ This is the changelog, summarising changes in each version(some minor changes ma
22

33
# 1.0
44

5+
### 1.6.0
6+
7+
Added `"adt_const_marker"` feature.
8+
9+
Added `BoolWit` in `typewit::const_marker`
10+
11+
Added these structs in `typewit::const_marker`(under the `"adt_const_marker"` feature):
12+
- `Str` (unit struct)
13+
14+
Added these structs in `typewit::const_marker::slice`(under the `"adt_const_marker"` feature):
15+
- `BoolSlice` (unit struct)
16+
- `CharSlice` (unit struct)
17+
- `I8Slice` (unit struct)
18+
- `I16Slice` (unit struct)
19+
- `I32Slice` (unit struct)
20+
- `I64Slice` (unit struct)
21+
- `I128Slice` (unit struct)
22+
- `IsizeSlice` (unit struct)
23+
- `StrSlice` (unit struct)
24+
- `U8Slice` (unit struct)
25+
- `U16Slice` (unit struct)
26+
- `U32Slice` (unit struct)
27+
- `U64Slice` (unit struct)
28+
- `U128Slice` (unit struct)
29+
- `UsizeSlice` (unit struct)
30+
31+
Changed `"nightly_mut_refs*"` crate feature to enable the `"rust_stable"` feature.
32+
533
### 1.5.0
634

735
Added support for these to `simple_type_witness` macro:

README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,16 +229,21 @@ These are the features of this crates:
229229

230230
- `"alloc"`: enable items that use anything from the standard `alloc` crate.
231231

232-
- `"const_marker"`(enabled by default): Enables the [`const_marker`] module,
232+
- `"const_marker"`(enabled by default): enables the [`const_marker`] module,
233233
and all items that depend on it.
234234

235+
- `"adt_const_marker"`(requires the nightly compiler):
236+
enables the `"rust_stable"` and `"const_marker"` crate features,
237+
and marker types in the [`const_marker`] module that have
238+
non-primitive `const` parameters.
239+
235240
- `"mut_refs"`: turns functions that take mutable references into const fns.
236-
note: as of July 2023,
241+
note: as of September 2023,
237242
this crate feature requires a stable compiler from the future.
238243

239244
- `"nightly_mut_refs"`(requires the nightly compiler):
240-
Enables the `"mut_refs"` crate feature and
241-
the `const_mut_refs` nightly feature.
245+
Enables the `"rust_stable"` and `"mut_refs"` crate features,
246+
and the `const_mut_refs` nightly feature.
242247

243248
None of the crate features are enabled by default.
244249

src/all_init_bytes.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#[cfg(test)]
2+
mod tests;
3+
4+
/// Marker trait for type for which all bytes are initialized
5+
///
6+
/// # Safety
7+
///
8+
/// All of the bytes in this type must be initialized.
9+
/// Uninitialized bytes includes padding and `MaybeUninit` fields.
10+
pub(crate) unsafe trait AllInitBytes {}
11+
12+
#[allow(dead_code)]
13+
pub(crate) const fn as_bytes<T: AllInitBytes>(reff: &T) -> &[u8] {
14+
unsafe {
15+
core::slice::from_raw_parts (
16+
reff as *const T as *const u8,
17+
core::mem::size_of::<T>(),
18+
)
19+
}
20+
}
21+
pub(crate) const fn slice_as_bytes<T: AllInitBytes>(reff: &[T]) -> &[u8] {
22+
unsafe {
23+
core::slice::from_raw_parts (
24+
reff.as_ptr() as *const u8,
25+
reff.len() * core::mem::size_of::<T>(),
26+
)
27+
}
28+
}
29+
30+
unsafe impl AllInitBytes for bool {}
31+
unsafe impl AllInitBytes for char {}
32+
unsafe impl AllInitBytes for u8 {}
33+
unsafe impl AllInitBytes for u16 {}
34+
unsafe impl AllInitBytes for u32 {}
35+
unsafe impl AllInitBytes for u64 {}
36+
unsafe impl AllInitBytes for u128 {}
37+
unsafe impl AllInitBytes for usize {}
38+
unsafe impl AllInitBytes for i8 {}
39+
unsafe impl AllInitBytes for i16 {}
40+
unsafe impl AllInitBytes for i32 {}
41+
unsafe impl AllInitBytes for i64 {}
42+
unsafe impl AllInitBytes for i128 {}
43+
unsafe impl AllInitBytes for isize {}
44+

src/all_init_bytes/tests.rs

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
use super::{as_bytes, slice_as_bytes};
2+
3+
4+
macro_rules! test_int {
5+
($vals:expr) => ({
6+
for val in $vals {
7+
assert_eq!(as_bytes(&val), val.to_ne_bytes());
8+
9+
let mut vec = arrayvec::ArrayVec::<u8, 32>::new();
10+
vec.try_extend_from_slice(&val.to_ne_bytes()).unwrap();
11+
vec.try_extend_from_slice(&val.to_ne_bytes()).unwrap();
12+
13+
assert_eq!(slice_as_bytes(&[val, val]), &vec[..]);
14+
}
15+
});
16+
}
17+
18+
macro_rules! test_signed_int {
19+
($vals:expr) => ({
20+
for val in $vals {
21+
test_int!{[val, -val]}
22+
}
23+
});
24+
}
25+
26+
27+
#[test]
28+
fn test_bool_as_bytes() {
29+
for b in [false, true] {
30+
assert_eq!(as_bytes(&b), &[b as u8][..]);
31+
assert_eq!(slice_as_bytes(&[b]), &[b as u8][..]);
32+
}
33+
}
34+
35+
#[test]
36+
fn test_char_as_bytes() {
37+
for c in ['f', 'o', '\n', '\t', 'ñ', '个', '\u{100000}', char::MAX] {
38+
assert_eq!(as_bytes(&c), (c as u32).to_ne_bytes());
39+
assert_eq!(slice_as_bytes(&[c]), (c as u32).to_ne_bytes());
40+
}
41+
}
42+
43+
#[test]
44+
fn test_u8_as_bytes() {
45+
test_int!{[0u8, 233u8]}
46+
}
47+
48+
#[test]
49+
fn test_u16_as_bytes() {
50+
test_int!{[0u16, 500u16, 1000u16]}
51+
}
52+
53+
#[test]
54+
fn test_u32_as_bytes() {
55+
test_int!{[0u32, 500u32, 1000u32, 1_000_000u32, 1_000_000_000u32]}
56+
}
57+
58+
#[test]
59+
fn test_u64_as_bytes() {
60+
test_int!{[
61+
0u64, 500u64, 1000u64, 1_000_000u64, 1_000_000_000u64,
62+
1_000_000_000_000_000_000u64,
63+
]}
64+
}
65+
66+
#[test]
67+
fn test_u128_as_bytes() {
68+
test_int!{[
69+
0u128, 500u128, 1000u128, 1_000_000u128, 1_000_000_000u128,
70+
1_000_000_000_000_000_000u128, 1_000_000_000_000_000_000_000_000_000u128,
71+
1_000_000_000_000_000_000_000_000_000_000_000_000u128,
72+
]}
73+
}
74+
75+
#[test]
76+
fn test_usize_as_bytes() {
77+
test_int!{[0usize, 500usize, 1000usize, 12345usize]}
78+
}
79+
80+
81+
#[test]
82+
fn test_i8_as_bytes() {
83+
test_signed_int!{[0i8, 111i8]}
84+
}
85+
86+
#[test]
87+
fn test_i16_as_bytes() {
88+
test_signed_int!{[0i16, 500i16, 1000i16]}
89+
}
90+
91+
#[test]
92+
fn test_i32_as_bytes() {
93+
test_signed_int!{[0i32, 500i32, 1000i32, 1_000_000i32, 1_000_000_000i32]}
94+
}
95+
96+
#[test]
97+
fn test_i64_as_bytes() {
98+
test_signed_int!{[
99+
0i64, 500i64, 1000i64, 1_000_000i64, 1_000_000_000i64,
100+
1_000_000_000_000_000_000i64,
101+
]}
102+
}
103+
104+
#[test]
105+
fn test_i128_as_bytes() {
106+
test_signed_int!{[
107+
0i128, 500i128, 1000i128, 1_000_000i128, 1_000_000_000i128,
108+
1_000_000_000_000_000_000i128, 1_000_000_000_000_000_000_000_000_000i128,
109+
1_000_000_000_000_000_000_000_000_000_000_000_000i128,
110+
]}
111+
}
112+
113+
#[test]
114+
fn test_isize_as_bytes() {
115+
test_signed_int!{[0isize, 500isize, 1000isize, 12345isize]}
116+
}

src/const_marker.rs

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,51 @@ use crate::{
3838
TypeNe,
3939
};
4040

41+
mod const_witnesses;
4142

43+
pub use const_witnesses::*;
44+
45+
#[cfg(feature = "adt_const_marker")]
46+
mod slice_const_markers;
47+
48+
#[cfg(feature = "adt_const_marker")]
49+
#[cfg_attr(feature = "docsrs", doc(cfg(feature = "adt_const_marker")))]
50+
pub use slice_const_markers::Str;
51+
52+
/// Marker types for `const FOO: &'static [T]` parameters.
53+
#[cfg(feature = "adt_const_marker")]
54+
#[cfg_attr(feature = "docsrs", doc(cfg(feature = "adt_const_marker")))]
55+
pub mod slice {
56+
pub use super::slice_const_markers::{
57+
BoolSlice,
58+
CharSlice,
59+
U8Slice,
60+
U16Slice,
61+
U32Slice,
62+
U64Slice,
63+
U128Slice,
64+
UsizeSlice,
65+
I8Slice,
66+
I16Slice,
67+
I32Slice,
68+
I64Slice,
69+
I128Slice,
70+
IsizeSlice,
71+
StrSlice,
72+
};
73+
}
74+
75+
76+
macro_rules! __const_eq_with {
77+
($L:ident, $R:ident) => {
78+
$L == $R
79+
};
80+
($L:ident, $R:ident, ($L2:ident, $R2:ident) $cmp:expr) => ({
81+
let $L2 = $L;
82+
let $R2 = $R;
83+
$cmp
84+
});
85+
} pub(crate) use __const_eq_with;
4286

4387
macro_rules! declare_const_param_type {
4488
(
@@ -47,7 +91,7 @@ macro_rules! declare_const_param_type {
4791

4892
$(
4993
$(#[$eq_docs:meta])*
50-
fn eq;
94+
fn eq $(($L:ident, $R:ident) $comparator:block)?;
5195
)?
5296
) => {
5397
#[doc = concat!(
@@ -80,7 +124,11 @@ macro_rules! declare_const_param_type {
80124
const EQ: Result<
81125
TypeEq<$struct<L>, $struct<R>>,
82126
TypeNe<$struct<L>, $struct<R>>,
83-
> = if L == R {
127+
> = if crate::const_marker::__const_eq_with!(
128+
L,
129+
R
130+
$($(, ($L, $R) $comparator)?)?
131+
) {
84132
// SAFETY: `L == R` (both are std types with sensible Eq impls)
85133
// therefore `$struct<L> == $struct<R>`
86134
unsafe {
@@ -100,10 +148,21 @@ macro_rules! declare_const_param_type {
100148
}
101149

102150
};
103-
}
151+
} pub(crate) use declare_const_param_type;
104152

105153

106-
declare_const_param_type!{Bool(bool)}
154+
declare_const_param_type!{
155+
Bool(bool)
156+
157+
///
158+
/// For getting a type witness that
159+
/// `Bool<B>` is either `Bool<true>` or `Bool<false>`,
160+
/// you can use [`BoolWit`].
161+
162+
163+
///
164+
fn eq;
165+
}
107166
declare_const_param_type!{Char(char)}
108167

109168
declare_const_param_type!{U8(u8)}

0 commit comments

Comments
 (0)