Skip to content

Commit 1c7b232

Browse files
Cleanup
1 parent fdbba9d commit 1c7b232

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed

src/lib.rs

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,23 @@ use core::marker::PhantomData;
3434
/// fn caller<'a>(r: &'a u8) { let u = 0u8; callee(NonDeDuplicatedStatic::new(uref)); }
3535
/// ```
3636
///
37-
/// But, by requiring `NonDeDuplicated`'s generic parameter `T` to implement [Any] the first
38-
/// example above fails, too. That prevents mistakes earlier.
37+
/// But, by requiring `NonDeDuplicatedFlexible`'s generic parameter `FROM` (or `NonDeDuplicated`'s
38+
/// generic parameter `T`) to implement [Any] the first example above fails, too. That prevents
39+
/// mistakes earlier.
40+
///
41+
/// Do not use [NonDeDuplicatedFlexible] directly. Instead, use [NonDeDuplicated],
42+
/// [NonDeDuplicatedStr] and [NonDeDuplicatedCStr].
3943
#[repr(transparent)]
40-
pub struct NonDeDuplicatedFlexible<FROM, OWN: Any + Send + Sync, TO: Any + ?Sized> {
44+
pub struct NonDeDuplicatedFlexible<FROM, OWN: Any + Send + Sync, TO: Any + Send + Sync + ?Sized> {
4145
cell: Cell<OWN>,
4246
_f: PhantomData<FROM>,
4347
_t: PhantomData<TO>,
4448
}
4549

46-
pub type NonDeDuplicated<T> = NonDeDuplicatedFlexible<T, T, T>;
50+
/// For non-de-duplicated objects stored in `static` variables. NOT for string slices - for those
51+
/// use [NonDeDuplicatedStr] and [NonDeDuplicatedCStr].
52+
#[allow(type_alias_bounds)]
53+
pub type NonDeDuplicated<T: Any + Send + Sync> = NonDeDuplicatedFlexible<T, T, T>;
4754

4855
impl<T: Any + Send + Sync> NonDeDuplicated<T> {
4956
/// Construct a new instance.
@@ -64,8 +71,10 @@ impl<T: Any + Send + Sync> NonDeDuplicated<T> {
6471
}
6572
}
6673

67-
pub type NonDeDuplicatedStr<const N: usize> = NonDeDuplicatedFlexible<&'static str, [u8; N], str>;
68-
impl<const N: usize> NonDeDuplicatedStr<N> {
74+
/// For non-de-duplicated string slices stored in `static` variables.
75+
pub type NonDeDuplicatedStr<'from, const N: usize> =
76+
NonDeDuplicatedFlexible<&'from str, [u8; N], str>;
77+
impl<'from, const N: usize> NonDeDuplicatedStr<'from, N> {
6978
/// Construct a new instance.
7079
pub const fn new(s: &str) -> Self {
7180
if s.len() > N {
@@ -117,23 +126,25 @@ impl<const N: usize> NonDeDuplicatedStr<N> {
117126
}
118127
}
119128

120-
/// For now, [Sync] requires that `OWN` is both [Sync] AND [Send], following
129+
/// For now, [Sync] (and [NonDeDuplicatedFlexible] in general) requires that `OWN` is both [Sync]
130+
/// AND [Send], following
121131
/// [std::sync::Mutex](https://doc.rust-lang.org/nightly/std/sync/struct.Mutex.html#impl-Sync-for-Mutex%3CT%3E).
122132
/// However, from <https://doc.rust-lang.org/nightly/core/marker/trait.Sync.html> it seems that `T:
123133
/// Send` may be unnecessary? Please advise.
124134
///
125-
/// Either way, [NonDeDuplicated] exists specifically for static variables. Those get never moved
126-
/// out. So, unlike [std::sync::Mutex], [NonDeDuplicated] itself doesn't need to implement [Send].
135+
/// Either way, [NonDeDuplicated], [NonDeDuplicatedStr] and [NonDeDuplicatedCStr] (and underlying
136+
/// [NonDeDuplicatedFlexible]) exist specifically for static variables. Those get never moved out.
137+
/// So, (unlike [std::sync::Mutex]) they do **not** need to implement [Send].
127138
///
128139
/// Also, unclear if `TO` needs to be [Send] and [Sync].
129-
unsafe impl<FROM, OWN: Any + Send + Sync, TO: Any + ?Sized> Sync
140+
unsafe impl<FROM, OWN: Any + Send + Sync, TO: Any + Send + Sync + ?Sized> Sync
130141
for NonDeDuplicatedFlexible<FROM, OWN, TO>
131142
{
132143
}
133144

134-
/// [NonDeDuplicated] is intended for `static` (immutable) variables only. So [Drop::drop] panics in
135-
/// debug/miri builds.
136-
impl<FROM, OWN: Any + Send + Sync, TO: Any + ?Sized> Drop
145+
/// [NonDeDuplicated] and friends are intended for `static` (immutable) variables only. So
146+
/// [Drop::drop] panics in debug/miri builds.
147+
impl<FROM, OWN: Any + Send + Sync, TO: Any + Send + Sync + ?Sized> Drop
137148
for NonDeDuplicatedFlexible<FROM, OWN, TO>
138149
{
139150
fn drop(&mut self) {

0 commit comments

Comments
 (0)