@@ -37,10 +37,8 @@ However, there is a problem (caused by de-duplication in `release`, and for some
3737or ` miri). It affects ("ordinary") ` const` values/expressions that equal in value to any ` static`
3838(whether it's a ` static ` variable, or a static literal), which may be your designated ` static ` .
3939Rust/LLVM re-uses address of one such matching ` static ` for references to any equal value(s) defined
40- as ` const ` . See a test [ ` src/lib.rs ` ->
41- ` addresses_not_unique_between_const_and_static() ` ] ( https://github.com/peter-lyons-kehl/ndd/blob/26d743d9b7bbaf41155e00174f8827efca5d5f32/src/lib.rs#L95 ) .
42- Such ` const ` , ` static ` or literal could be in 3rd party code, even private. (See
43- [ ` cross_crate_demo_bug/ ` ] ( https://github.com/peter-lyons-kehl/ndd/tree/main/cross_crate_demo_bug ) )!
40+ as ` const ` . See [ ` src/lib.rs ` -> ` u8_global_const_and_global_static_release() ` ] . Such ` const ` ,
41+ ` static ` or literal could be in 3rd party code, even private. (See [ ` cross_crate_demo_bug/ ` ] .)
4442
4543Things get worse: ` dev ` builds don't have this consistent:
4644
@@ -71,29 +69,30 @@ opt-level = 2
7169
7270## Solution
7371
74- ` ndd:NonDeDuplicated ` uses
75- [ ` core::cell::Cell ` ] ( https://doc.rust-lang.org/nightly/core/cell/struct.Cell.html ) to hold the data
76- passed in by the user. There is no mutation and no mutation access. The only access it gives to the
77- inner data is through shared references.
72+ [ ` ndd::NonDeDuplicated ` ] uses [ ` core::cell::Cell ` ] to hold the data passed in by the user. There is no
73+ mutation and no mutation access. The only access it gives to the inner data is through shared
74+ references.
7875
79- Unlike ` Cell ` (and friends), ` NonDeDuplicated ` ** does** implement
80- [ ` core::marker::Sync ` ] ( https://doc.rust-lang.org/nightly/core/marker/trait.Sync.html ) (if the inner
81- data's type implements ` Send ` and ` Sync ` ). It can safely do so, because it never provides mutable
82- access, and it never mutates the inner data. That is similar to how
83- [ ` std::sync::Mutex ` ] ( https://doc.rust-lang.org/nightly/std/sync/struct.Mutex.html#impl-Sync-for-Mutex%3CT%3E )
76+ Unlike ` Cell ` (and friends), ` NonDeDuplicated ` ** does** implement [ ` core::marker::Sync ` ] (if the
77+ inner data's type implements ` Send ` and ` Sync ` ). It can safely do so, because it never provides
78+ mutable access, and it never mutates the inner data. That is similar to how [ ` std::sync::Mutex ` ]
8479implements ` Sync ` , too.
8580
86- See [ ` src/lib.rs ` ->
87- ` tests_behavior_with_ndd ` ] ( https://github.com/search?q=repo%3Apeter-lyons-kehl%2Fndd+tests_behavior_with_ndd+path%3Asrc%2Flib.rs&type=code ) .
81+ See [ ` src/lib.rs ` -> ` tests_behavior_with_ndd ` ] .
8882
8983## Use
9084
91- Use ` ndd::NonDeDuplicated ` to wrap your static data. Use it for (immutable) ` static ` variables only.
92- Do ** not** use it for locals or on heap. That is validated by implementation of
93- [ core::ops::Drop] ( https://doc.rust-lang.org/nightly/core/ops/trait.Drop.html ) , which ` panic ` -s in
94- ` dev ` builds.
85+ Use [ ` ndd::NonDeDuplicated ` ] to wrap your static data (other than string literals or C string
86+ literals). Use it for (immutable) ` static ` variables only. Do ** not** use it for locals or on heap.
87+ That is validated by implementation of [ ` core::ops::Drop ` ] , which ` panic ` s in ` dev ` builds.
9588
96- See unit tests in [ src/lib.rs] ( src/lib.rs ) .
89+ Use [ ` ndd::NonDeDuplicatedStr ` ] and [ ` ndd::NonDeDuplicatedCStr ` ] to wrap static string slices
90+ (` &str ` ) and C strings (owned bytes that defer to ` &CStr ` ). These two types need a ` const ` generic
91+ parameter, which is the length (in bytes). There is no way around this (in stable Rust). On
92+ ` nightly ` Rust you can use ` ndd::infer:NonDeDuplicatedStr ` and ` ndd::infer:NonDeDuplicatedCStr ` from
93+ ** odd-numbered** (` -nightly ` ) version of ` ndd ` instead.
94+
95+ See unit tests in [ ` src/lib.rs ` ] , and [ ` cross_crate_demo_fix/callee/src/lib.rs ` ] .
9796
9897## Compatibility
9998
@@ -103,7 +102,7 @@ See unit tests in [src/lib.rs](src/lib.rs).
103102
104103### Stable is always forward compatible
105104
106- ` ndd ` is planned to be always below version ` 1.0 ` . (If a need arises for big incompatible
105+ ` ndd ` is planned to be always below version ` 1.0 ` . (If a need ever arises for big incompatible
107106functionality, that can go in a new crate.)
108107
109108That allows you to specify ` ndd ` as a dependency with version ` 0.* ` , which will match ANY ** major**
@@ -154,7 +153,7 @@ versions `1.0` or higher.
154153 trick] ( https://github.com/dtolnay/semver-trick ) . See also [ The Cargo Book > Dependency
155154 Resolution] ( https://rustwiki.org/en/cargo/reference/resolver.html#version-incompatibility-hazards ) .
156155
157- However, the only type exported from ` ndd ` is ` ndd::NonDeDuplicated ` . It is a zero-cost wrapper
156+ However, the only type exported from ` ndd ` is [ ` ndd::NonDeDuplicated ` ] . It is a zero-cost wrapper
158157 suitable for immutable ` static ` variables. It is normally not being passed around as a
159158 parameter/return type or a composite type. And its functions can get inlined/optimized away. So,
160159 there shouldn't be any big binary size/speed difference, or usability difference, if there happen
@@ -206,29 +205,26 @@ use an asterisk mask for the minor version, like `0.2.*`. But then you lose auto
206205
207206### Nightly functionality
208207
209- Functionality of odd-numbered major (` -nightly ` ) versions is always subject to change.
208+ WARNING: Functionality of odd-numbered major (` -nightly ` ) versions is always subject to change!
210209
211- The following extra functionality is available on ` 0.3.1 -nightly ` . You need ` nightly ` Rust toolchain
212- (of course) .
210+ The following extra functionality is available on ` 0.3.5 -nightly ` . Of course, you need ` nightly `
211+ Rust toolchain .
213212
214213#### as_array_of_cells
215214
216- ` ndd::NonDeDuplicated ` has function ` as_array_of_cells ` , similar to Rust's
217- [ ` core::cell::Cell::as_array_of_cells ` ] ( https://doc.rust-lang.org/nightly/core/cell/struct.Cell.html#method.as_array_of_cells )
218- (which will, hopefully, become stable in 1.91).
215+ [ ` ndd::NonDeDuplicated ` ] has function ` as_array_of_cells ` , similar to Rust's
216+ [ ` core::cell::Cell::as_array_of_cells ` ] (which will, hopefully, become stable in Rust 1.91).
219217
220218#### as_slice_of_cells
221219
222- Similar to ` as_array_of_cells ` , ` ndd::NonDeDuplicated ` has function ` as_slice_of_cells ` . That
220+ Similar to ` as_array_of_cells ` , [ ` ndd::NonDeDuplicated ` ] has function ` as_slice_of_cells ` . That
223221** can** be stable with with Rust ` 1.88 ` +. However, to simplify versioning, it's bundled in
224222` -nightly ` together with ` as_array_of_cells ` . If you need it earlier, get in touch.
225223
226224#### const Deref and From
227225
228- [ core::ops::Deref] ( https://doc.rust-lang.org/nightly/core/ops/trait.Deref.html ) and
229- [ core::convert::From] ( https://doc.rust-lang.org/nightly/core/convert/trait.From.html ) are
230- implemented as ` const ` . As of mid 2025, ` const ` traits are having high traction in Rust. Hopefully
231- this will be stable not in years, but sooner.
226+ [ ` core::ops::Deref ` ] and [ ` core::convert::From ` ] are implemented as ` const ` . As of mid 2025, ` const `
227+ traits are having high traction in Rust. Hopefully this will be stable not in years, but sooner.
232228
233229These traits are ** not** implemented in stable versions at all. Why? Because ` ndd ` types are
234230intended for ` static ` variables, so non-` const ` functions don't help us.
@@ -287,14 +283,32 @@ Used by
287283## Updates
288284
289285Please subscribe for low frequency updates at
290- [ #2 ] ( https://github.com/peter-lyons-kehl/ndd/issues/2 ) .
286+ [ peter-lyons-kehl/ndd/issues #2] ( https://github.com/peter-lyons-kehl/ndd/issues/2 ) .
291287
292288## Side fruit
293289
294290The following side fruit is ` std ` -only, but related: ` std::sync::mutex::data_ptr(&self) ` is now
295291` const ` function: pull request
296292[ rust-lang/rust #146904 ] ( https://github.com/rust-lang/rust/pull/146904 ) .
297293
294+ <!-- Link URLs -->
298295[ `core::ptr::eq` ] : https://doc.rust-lang.org/1.86.0/core/ptr/fn.eq.html
299296[ `core::ptr::addr_eq` ] : https://doc.rust-lang.org/1.86.0/core/ptr/fn.addr_eq.html
297+ <!-- The following works also on https://crates.io/crates/ndd auto-magically: -->
300298[ `src/lib.rs` -> `addresses_unique_between_statics()` ] : src/lib.rs#L246
299+ [ `src/lib.rs` -> `u8_global_const_and_global_static_release()` ] : src/lib.rs#L258
300+ [ `cross_crate_demo_bug/` ] : cross_crate_demo_bug/
301+ [ `core::cell::Cell` ] : https://doc.rust-lang.org/1.86.0/core/cell/struct.Cell.html
302+ [ `core::marker::Sync` ] : https://doc.rust-lang.org/1.86.0/core/marker/trait.Sync.html
303+ [ `std::sync::Mutex` ] : https://doc.rust-lang.org/1.86.0/std/sync/struct.Mutex.html#impl-Sync-for-Mutex<T>
304+ [ `src/lib.rs` -> `tests_behavior_with_ndd` ] : src/lib.rs#L339
305+ [ `core::ops::Drop` ] : https://doc.rust-lang.org/1.86.0/core/ops/trait.Drop.html
306+ [ `src/lib.rs` ] : src/lib.rs
307+ [ `ndd::NonDeDuplicated` ] : https://docs.rs/ndd/latest/ndd/type.NonDeDuplicated.html
308+ [ `ndd::NonDeDuplicatedStr` ] : https://docs.rs/ndd/latest/ndd/type.NonDeDuplicatedStr.html
309+ [ `ndd::NonDeDuplicatedCStr` ] : https://docs.rs/ndd/latest/ndd/type.NonDeDuplicatedCStr.html
310+ [ `cross_crate_demo_fix/callee/src/lib.rs` ] : cross_crate_demo_fix/callee/src/lib.rs
311+ <!-- nightly-only: -->
312+ [ `core::cell::Cell::as_array_of_cells` ] : https://doc.rust-lang.org/nightly/core/cell/struct.Cell.html#method.as_array_of_cells
313+ [ `core::ops::Deref` ] : https://doc.rust-lang.org/nightly/core/ops/trait.Deref.html
314+ [ `core::convert::From` ] : https://doc.rust-lang.org/nightly/core/convert/trait.From.html
0 commit comments