Skip to content

Commit c08592f

Browse files
Refine some staticstring! macro related stuff a bit more
1 parent 6810caa commit c08592f

File tree

3 files changed

+22
-10
lines changed

3 files changed

+22
-10
lines changed

demo/string_demo.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,16 @@ fn main() {
9393
println!("Value: {}", STATIC_UNICODE);
9494
println!("Debug info: {:?}", STATIC_UNICODE);
9595
println!("Length: {}", STATIC_UNICODE.len());
96-
println!("Remaining capacity: {}", STATIC_UNICODE.remaining_capacity());
96+
println!(
97+
"Remaining capacity: {}",
98+
STATIC_UNICODE.remaining_capacity()
99+
);
97100
let runtime_unicode = staticstring!("🙉🙉💣💣🙉🙉💣💣🙉🙉💣💣", 255);
98101
println!("Value: {}", runtime_unicode);
99102
println!("Debug info: {:?}", runtime_unicode);
100103
println!("Length: {}", runtime_unicode.len());
101-
println!("Remaining capacity: {}", runtime_unicode.remaining_capacity());
104+
println!(
105+
"Remaining capacity: {}",
106+
runtime_unicode.remaining_capacity()
107+
);
102108
}

src/lib.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1750,11 +1750,6 @@ impl<const N: usize> StaticVec<u8, N> {
17501750
#[doc(hidden)]
17511751
#[inline]
17521752
pub(crate) const fn bytes_to_data(values: &[u8]) -> MaybeUninit<[u8; N]> {
1753-
// This works at compile time too, of course.
1754-
assert!(
1755-
values.len() <= N,
1756-
"Attempted to create a `StaticString` with insufficient capacity from an `&str` literal!"
1757-
);
17581753
// What follows is an idea partially arrived at from reading the source of the `const-concat`
17591754
// crate. Note that it amounts to effectively a `const fn` compatible implementation of what
17601755
// `MaybeUninit::assume_uninit()` does, and is *only* used here due to there being no other way
@@ -1779,7 +1774,7 @@ impl<const N: usize> StaticVec<u8, N> {
17791774
// you, `const_loop`!
17801775
let mut i = 0;
17811776
while i < values.len() {
1782-
// We've statically asserted that `values.len() <= N` at the top of this overall function,
1777+
// We've statically asserted that `values.len() <= N` before entering this overall function,
17831778
// so there's no concern that we might go out of bounds here (although that would still just
17841779
// result in compilation not actually succeeding at all due to the `const` index error).
17851780
res[i] = MaybeUninit::new(values[i]);
@@ -1796,6 +1791,15 @@ impl<const N: usize> StaticVec<u8, N> {
17961791
#[doc(hidden)]
17971792
#[inline(always)]
17981793
pub const fn __new_from_const_str(values: &str) -> Self {
1794+
// This works at compile time too, of course, thanks to the `const_panic` feature.
1795+
assert!(
1796+
values.len() <= N,
1797+
// At the moment, I don't think this message is actually printed in any context when the
1798+
// assertion gets triggered (currently it's just "could not evaluate static initializer") but
1799+
// I feel like it doesn't hurt to have here just in case the compiler-error behavior changes
1800+
// such that custom messages are actually shown.
1801+
"Attempted to create a `StaticString` with insufficient capacity from an `&str` literal!"
1802+
);
17991803
Self::new_from_str_data(Self::bytes_to_data(values.as_bytes()), values.len())
18001804
}
18011805
}

src/macros.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ macro_rules! staticvec {
6363
/// // keeping in mind that length is measured in bytes and not characters of course:
6464
/// const S4: StaticString<36> = staticstring!("BC🤔BC🤔BC🤔", 36);
6565
/// assert_eq!(S4, "BC🤔BC🤔BC🤔");
66+
/// assert_eq!(S4.len(), 18);
67+
/// assert_eq!(S4.capacity(), 36);
6668
/// ```
6769
///
6870
/// Note that attempting to explicitly provide a capacity that is less than the number of bytes
@@ -90,8 +92,8 @@ macro_rules! staticstring {
9092
};};
9193
($val:expr, $n:expr) => {{
9294
// In this scenario, an actual assertion inside of `StaticVec::bytes_to_data`
93-
// (available at compile time thanks to the `const_panic` feature)
94-
// handles ensuring that `$val.len() <= N` for us.
95+
// (available at compile time thanks to the `const_panic` feature) handles
96+
// ensuring that `$val.len() <= N` for us.
9597
unsafe {
9698
$crate::StaticString::<$n>::__new_from_staticvec(
9799
$crate::StaticVec::<u8, $n>::__new_from_const_str($val)

0 commit comments

Comments
 (0)