Skip to content

Commit ad399e7

Browse files
WIP NIGHTLY-only: NddStr
1 parent ceae38e commit ad399e7

File tree

1 file changed

+33
-17
lines changed

1 file changed

+33
-17
lines changed

src/lib.rs

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#![doc = include_str!("../README.md")]
22
#![cfg_attr(not(any(doc, test)), no_std)]
3+
#![feature(adt_const_params)]
4+
#![feature(unsized_const_params)]
5+
#![feature(generic_const_exprs)]
36

47
use core::any::Any;
58
use core::cell::Cell;
@@ -35,23 +38,22 @@ use core::marker::PhantomData;
3538
/// fn caller<'a>(r: &'a u8) { let u = 0u8; callee(NonDeDuplicatedStatic::new(uref)); }
3639
/// ```
3740
///
38-
/// But, by requiring `NonDeDuplicatedFlexible`'s generic parameter `FROM` (or `NonDeDuplicated`'s
41+
/// But, by requiring [NonDeDuplicatedFlexible]'s generic parameter `OWN` (or [NonDeDuplicated]'s
3942
/// generic parameter `T`) to implement [Any] the first example above fails, too. That prevents
4043
/// mistakes earlier.
4144
///
4245
/// Do not use [NonDeDuplicatedFlexible] directly. Instead, use [NonDeDuplicated],
4346
/// [NonDeDuplicatedStr] and [NonDeDuplicatedCStr].
4447
#[repr(transparent)]
45-
pub struct NonDeDuplicatedFlexible<FROM, OWN: Any + Send + Sync, TO: Any + Send + Sync + ?Sized> {
48+
pub struct NonDeDuplicatedFlexible<OWN: Any + Send + Sync, TO: Any + Send + Sync + ?Sized> {
4649
cell: Cell<OWN>,
47-
_f: PhantomData<FROM>,
4850
_t: PhantomData<TO>,
4951
}
5052

5153
/// For non-de-duplicated objects stored in `static` variables. NOT for string slices - for those
5254
/// use [NonDeDuplicatedStr] and [NonDeDuplicatedCStr].
5355
#[allow(type_alias_bounds)]
54-
pub type NonDeDuplicated<T: Any + Send + Sync> = NonDeDuplicatedFlexible<T, T, T>;
56+
pub type NonDeDuplicated<T: Any + Send + Sync> = NonDeDuplicatedFlexible<T, T>;
5557

5658
impl<T: Any + Send + Sync> NonDeDuplicated<T> {
5759
/// Construct a new instance.
@@ -60,7 +62,6 @@ impl<T: Any + Send + Sync> NonDeDuplicated<T> {
6062
//Using core::hint::black_box() seems unnecessary.
6163
//cell: Cell::new(core::hint::black_box(value)),
6264
cell: Cell::new(value),
63-
_f: PhantomData,
6465
_t: PhantomData,
6566
}
6667
}
@@ -112,14 +113,12 @@ const fn bytes_to_array<const N: usize>(bytes: &[u8]) -> [u8; N] {
112113
}
113114

114115
/// For non-de-duplicated string slices stored in `static` variables.
115-
pub type NonDeDuplicatedStr<'from, const N: usize> =
116-
NonDeDuplicatedFlexible<&'from str, [u8; N], str>;
117-
impl<'from, const N: usize> NonDeDuplicatedStr<'from, N> {
116+
pub type NonDeDuplicatedStr<const N: usize> = NonDeDuplicatedFlexible<[u8; N], str>;
117+
impl<const N: usize> NonDeDuplicatedStr<N> {
118118
/// Construct a new instance.
119119
pub const fn new(s: &str) -> Self {
120120
Self {
121121
cell: Cell::new(bytes_to_array(s.as_bytes())),
122-
_f: PhantomData,
123122
_t: PhantomData,
124123
}
125124
}
@@ -139,15 +138,32 @@ impl<'from, const N: usize> NonDeDuplicatedStr<'from, N> {
139138
}
140139
}
141140

141+
#[repr(transparent)]
142+
struct NddStr<const S: &'static str>(NonDeDuplicatedStr<{ S.len() }>)
143+
where
144+
[(); S.len()]: Any;
145+
impl<'from, const S: &'static str> NddStr<S>
146+
where
147+
[(); S.len()]: Any,
148+
{
149+
pub const fn new() -> Self {
150+
Self(NonDeDuplicatedStr::new(S))
151+
}
152+
153+
pub const fn get(&self) -> &str {
154+
self.0.get()
155+
}
156+
}
157+
static NDShehe1: NddStr<"hehe"> = NddStr(NonDeDuplicatedStr::new("hehe"));
158+
static NDShehe2: NddStr<"hehe"> = NddStr::new();
159+
142160
/// For non-de-duplicated string slices stored in `static` variables.
143-
pub type NonDeDuplicatedCStr<'from, const N: usize> =
144-
NonDeDuplicatedFlexible<&'from CStr, [u8; N], CStr>;
145-
impl<'from, const N: usize> NonDeDuplicatedCStr<'from, N> {
161+
pub type NonDeDuplicatedCStr<const N: usize> = NonDeDuplicatedFlexible<[u8; N], CStr>;
162+
impl<const N: usize> NonDeDuplicatedCStr<N> {
146163
/// Construct a new instance.
147164
pub const fn new(s: &CStr) -> Self {
148165
Self {
149166
cell: Cell::new(bytes_to_array(s.to_bytes())),
150-
_f: PhantomData,
151167
_t: PhantomData,
152168
}
153169
}
@@ -178,15 +194,15 @@ impl<'from, const N: usize> NonDeDuplicatedCStr<'from, N> {
178194
/// So, (unlike [std::sync::Mutex]) they do **not** need to implement [Send].
179195
///
180196
/// Also, unclear if `TO` needs to be [Send] and [Sync].
181-
unsafe impl<FROM, OWN: Any + Send + Sync, TO: Any + Send + Sync + ?Sized> Sync
182-
for NonDeDuplicatedFlexible<FROM, OWN, TO>
197+
unsafe impl<OWN: Any + Send + Sync, TO: Any + Send + Sync + ?Sized> Sync
198+
for NonDeDuplicatedFlexible<OWN, TO>
183199
{
184200
}
185201

186202
/// [NonDeDuplicated] and friends are intended for `static` (immutable) variables only. So
187203
/// [Drop::drop] panics in debug/miri builds.
188-
impl<FROM, OWN: Any + Send + Sync, TO: Any + Send + Sync + ?Sized> Drop
189-
for NonDeDuplicatedFlexible<FROM, OWN, TO>
204+
impl<OWN: Any + Send + Sync, TO: Any + Send + Sync + ?Sized> Drop
205+
for NonDeDuplicatedFlexible<OWN, TO>
190206
{
191207
fn drop(&mut self) {
192208
// If the client uses Box::leak() or friends, then drop() will NOT happen. That is OK: A

0 commit comments

Comments
 (0)