re_string_interner 0.36.0

Yet another string interning library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
//! Yet another string interning library.
//!
//! The main thing that makes this library different is that
//! [`InternedString`] stores the hash of the string, which makes
//! using it in lookups is really fast, especially when using [`nohash_hasher::IntMap`].
//!
//! The hash is assumed to be perfect, which means this library accepts the risk of hash collisions!
//!
//! The interned strings are never freed, so don't intern too many things.

pub mod external {
    pub use nohash_hasher;
    pub use paste;
    pub use serde;
}

/// Fast but high quality string hash
#[inline]
fn hash(value: impl std::hash::Hash) -> u64 {
    use std::hash::Hasher as _;
    // Don't use ahash::AHasher::default() since it uses a random number for seeding the hasher on every application start.
    let mut hasher =
        std::hash::BuildHasher::build_hasher(&ahash::RandomState::with_seeds(0, 1, 2, 3));
    value.hash(&mut hasher);
    hasher.finish()
}

// ----------------------------------------------------------------------------

#[derive(Copy, Clone, Eq, re_byte_size::SizeBytes)]
pub struct InternedString {
    hash: u64, // TODO(emilk): consider removing the hash from the `InternedString` (benchmark!)
    string: &'static str,
}

static_assertions::assert_not_impl_any!(InternedString: std::borrow::Borrow<str>);

impl InternedString {
    #[inline]
    pub fn new(string: &str) -> Self {
        global_intern(string)
    }

    #[inline]
    pub fn as_str(&self) -> &'static str {
        self.string
    }

    /// Precomputed hash of the string.
    #[inline]
    pub fn hash(&self) -> u64 {
        self.hash
    }
}

impl From<&str> for InternedString {
    #[inline]
    fn from(string: &str) -> Self {
        Self::new(string)
    }
}

impl From<String> for InternedString {
    #[inline]
    fn from(string: String) -> Self {
        Self::new(&string)
    }
}

impl From<&String> for InternedString {
    #[inline]
    fn from(string: &String) -> Self {
        Self::new(string)
    }
}

impl std::cmp::PartialEq for InternedString {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.hash == other.hash
    }
}

impl std::hash::Hash for InternedString {
    #[inline]
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        state.write_u64(self.hash);
    }
}

impl nohash_hasher::IsEnabled for InternedString {}

impl std::cmp::PartialOrd for InternedString {
    #[inline]
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl std::cmp::Ord for InternedString {
    #[inline]
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.string.cmp(other.string)
    }
}

impl AsRef<str> for InternedString {
    #[inline]
    fn as_ref(&self) -> &str {
        self.string
    }
}

impl std::ops::Deref for InternedString {
    type Target = str;

    #[inline]
    fn deref(&self) -> &str {
        self.as_str()
    }
}

impl std::fmt::Debug for InternedString {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.as_str().fmt(f)
    }
}

impl std::fmt::Display for InternedString {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.as_str().fmt(f)
    }
}

impl serde::Serialize for InternedString {
    #[inline]
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        self.as_str().serialize(serializer)
    }
}

impl<'de> serde::Deserialize<'de> for InternedString {
    #[inline]
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        String::deserialize(deserializer).map(|s| global_intern(&s))
    }
}

// ----------------------------------------------------------------------------

#[derive(Default)]
struct StringInterner {
    map: nohash_hasher::IntMap<u64, &'static str>,
}

impl StringInterner {
    #[cfg_attr(not(test), expect(dead_code))] // only used in tests
    pub fn len(&self) -> usize {
        self.map.len()
    }

    pub fn intern(&mut self, string: &str) -> InternedString {
        let hash = hash(string);

        let static_ref_string = self
            .map
            .entry(hash)
            .or_insert_with(|| Box::leak(Box::<str>::from(string)));

        InternedString {
            hash,
            string: static_ref_string,
        }
    }

    pub fn bytes_used(&self) -> usize {
        // size_of_val takes references to what it wants to measure,
        // and that is wat `ier()` gives us, so this is all correct.
        self.map
            .iter()
            .map(|(k, v): (_, &&str)| {
                std::mem::size_of_val(k) + std::mem::size_of::<&str>() + v.len()
            })
            .sum()
    }
}

// ----------------------------------------------------------------------------

/// Intern a string literal once and return the cached value on subsequent calls.
///
/// Use for hot paths that produce the same interned identifier every call. Without this,
/// each call hashes the literal and locks the global interner, which adds up on per-frame
/// invocations from visualizer `execute` methods, codegen accessors, etc.
///
/// `$ty` must be a type declared via [`declare_new_type!`] (or anything constructible via
/// `From<&'static str>`).
///
/// ```ignore
/// fn identifier() -> ViewSystemIdentifier {
///     re_string_interner::intern_static!(ViewSystemIdentifier, "Ellipsoids3D")
/// }
/// ```
#[macro_export]
macro_rules! intern_static {
    ($ty:ty, $lit:literal) => {{
        static CACHED: ::std::sync::LazyLock<$ty> =
            ::std::sync::LazyLock::new(|| <$ty as ::std::convert::From<&str>>::from($lit));
        *CACHED
    }};
}

/// Like [`intern_static!`], but for types declared via [`declare_new_type_nonempty!`].
///
/// Those types have no infallible `From<&str>`; instead they expose
/// `from_static_str`. The empty string is rejected **at compile time** here (the
/// literal is checked in a `const` context), so an empty literal is a build error
/// rather than a runtime panic.
///
/// ```ignore
/// fn identifier() -> ViewSystemIdentifier {
///     re_string_interner::intern_static_nonempty!(ViewSystemIdentifier, "Ellipsoids3D")
/// }
/// ```
///
/// A non-empty literal compiles:
/// ```
/// re_string_interner::declare_new_type_nonempty!(
///     /// A test identifier.
///     pub struct MyString;
/// );
/// let _ = re_string_interner::intern_static_nonempty!(MyString, "non_empty");
/// ```
///
/// An empty literal fails to compile:
/// ```compile_fail
/// re_string_interner::declare_new_type_nonempty!(
///     /// A test identifier.
///     pub struct MyString;
/// );
/// let _ = re_string_interner::intern_static_nonempty!(MyString, "");
/// ```
#[macro_export]
macro_rules! intern_static_nonempty {
    ($ty:ty, $lit:literal) => {{
        const _: () = assert!(!$lit.is_empty(), "empty string literal");
        static CACHED: ::std::sync::LazyLock<$ty> =
            ::std::sync::LazyLock::new(|| <$ty>::from_static_str($lit));
        *CACHED
    }};
}

/// Declare a newtype wrapper around [`InternedString`] with
/// all the convenience methods you would want.
///
/// Usage:
/// ```
/// re_string_interner::declare_new_type!(
///     /// My typesafe string
///     pub struct MyString;
/// );
/// ```
#[macro_export]
macro_rules! declare_new_type {
    (
        $(#[$meta:meta])* // capture docstrings; see https://stackoverflow.com/questions/33999341/generating-documentation-in-macros
        $vis:vis struct $StructName:ident;
    ) => {
        $(#[$meta])*
        #[derive(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
        pub struct $StructName($crate::InternedString);


        impl $StructName {
            #[inline]
            pub fn new(string: &str) -> Self {
                Self($crate::InternedString::new(string))
            }

            #[inline]
            pub fn as_str(&self) -> &'static str {
                self.0.as_str()
            }

            /// Precomputed hash of the string.
            #[inline]
            pub fn hash(&self) -> u64 {
                self.0.hash()
            }
        }

        impl $crate::external::nohash_hasher::IsEnabled for $StructName {}

        impl From<&str> for $StructName {
            #[inline]
            fn from(string: &str) -> Self {
                Self::new(string)
            }
        }

        impl From<String> for $StructName {
            #[inline]
            fn from(string: String) -> Self {
                Self::new(&string)
            }
        }

        impl AsRef<str> for $StructName {
            #[inline]
            fn as_ref(&self) -> &str {
                self.as_str()
            }
        }

        impl std::ops::Deref for $StructName {
            type Target = str;

            #[inline]
            fn deref(&self) -> &str {
                self.as_str()
            }
        }

        impl std::fmt::Debug for $StructName {
            #[inline]
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                self.as_str().fmt(f)
            }
        }

        impl std::fmt::Display for $StructName {
            #[inline]
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                self.as_str().fmt(f)
            }
        }

        impl<'a> PartialEq<&'a str> for $StructName {
            #[inline]
            fn eq(&self, other: &&'a str) -> bool {
                self.as_str() == *other
            }
        }

        impl<'a> PartialEq<&'a str> for &$StructName {
            #[inline]
            fn eq(&self, other: &&'a str) -> bool {
                self.as_str() == *other
            }
        }

        impl<'a> PartialEq<$StructName> for &'a str {
            #[inline]
            fn eq(&self, other: &$StructName) -> bool {
                *self == other.as_str()
            }
        }

        impl re_byte_size::SizeBytes for $StructName {
            const IS_POD: bool = true;

            #[inline]
            fn heap_size_bytes(&self) -> u64 {
                0
            }
        }
    };
}

/// Like [`declare_new_type!`], but the string is validated.
///
/// Currently the only rule is that the string must not be empty, but validation is centralized in
/// one place (a private `validate` fn) so further rules (e.g. no whitespace) can be added later
/// without changing the public API. The generated `Invalid<StructName>Error` carries the reason
/// the string was rejected.
///
/// Compared to [`declare_new_type!`], this:
/// - does **not** implement the infallible `From<&str>` (any lifetime) / `From<String>`, nor an
///   infallible `new`;
/// - instead exposes fallible `try_new(impl AsRef<str>)` (for any borrowed or owned string) and
///   `TryFrom<String>`, returning an `Invalid<StructName>Error` on an invalid string;
/// - generates that `Invalid<StructName>Error` error type (implements [`std::error::Error`]);
/// - exposes `from_static_str(&'static str)` which **panics** on an invalid string, for use with
///   [`intern_static_nonempty!`] and other trusted compile-time literals;
/// - implements `From<&'static str>` (delegating to `from_static_str`, so it **panics** on empty),
///   which keeps `impl Into<StructName>` parameters ergonomic for trusted string literals/consts
///   while still forcing dynamic `&str`/`String` through the fallible constructors;
/// - implements a validating [`serde::Deserialize`] (empty string ⇒ error), so empty values cannot
///   sneak back in through deserialization. **Do not** add a `serde::Deserialize`/`serde::Serialize`
///   derive in the passed-in attributes — they are provided here.
///
/// Usage:
/// ```
/// re_string_interner::declare_new_type_nonempty!(
///     /// My non-empty typesafe string
///     pub struct MyString;
/// );
/// assert!(MyString::try_new("").is_err());
/// assert_eq!(MyString::try_new("hi").unwrap().as_str(), "hi");
/// assert_eq!(MyString::from("hi").as_str(), "hi"); // `From<&'static str>`, for `impl Into` ergonomics
/// ```
#[macro_export]
macro_rules! declare_new_type_nonempty {
    (
        $(#[$meta:meta])* // capture docstrings; see https://stackoverflow.com/questions/33999341/generating-documentation-in-macros
        $vis:vis struct $StructName:ident;
    ) => {
        $crate::external::paste::paste! {
            $(#[$meta])*
            #[derive(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
            pub struct $StructName($crate::InternedString);

            #[doc = "Error returned when constructing an invalid [`" $StructName "`]."]
            #[derive(Clone, Copy, PartialEq, Eq)]
            pub struct [<Invalid $StructName Error>] {
                /// Why the string was rejected, e.g. `"must not be empty"`.
                reason: &'static str,
            }

            impl std::fmt::Display for [<Invalid $StructName Error>] {
                fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                    write!(f, concat!("Invalid `", stringify!($StructName), "`: {}"), self.reason)
                }
            }

            impl std::fmt::Debug for [<Invalid $StructName Error>] {
                fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                    write!(f, concat!("Invalid", stringify!($StructName), "Error({:?})"), self.reason)
                }
            }

            impl std::error::Error for [<Invalid $StructName Error>] {}

            impl $StructName {
                /// The single place where the naming rules are enforced.
                ///
                /// This is the single place where the naming rules are enforced.
                /// Currently only forbids the empty string, but this is where future rules
                /// (e.g. no whitespace) would go.
                #[inline]
                pub fn try_new(string: impl AsRef<str>) -> Result<Self, [<Invalid $StructName Error>]> {
                    let string = string.as_ref();
                    if string.is_empty() {
                        return Err([<Invalid $StructName Error>] { reason: "must not be empty" });
                    }
                    Ok(Self($crate::InternedString::new(string)))
                }

                /// Create from a trusted compile-time string literal.
                ///
                /// # Panics
                /// Panics if `string` is invalid (e.g. empty).
                #[inline]
                pub fn from_static_str(string: &'static str) -> Self {
                    match Self::try_new(string) {
                        Ok(slf) => slf,
                        Err(err) => panic!("{err} (got {string:?})"),
                    }
                }

                #[inline]
                pub fn as_str(&self) -> &'static str {
                    self.0.as_str()
                }

                /// Precomputed hash of the string.
                #[inline]
                pub fn hash(&self) -> u64 {
                    self.0.hash()
                }
            }

            impl $crate::external::nohash_hasher::IsEnabled for $StructName {}

            // NOTE: no `TryFrom<&str>` / `TryFrom<&String>`: those would collide with the blanket
            // `impl<U: Into<T>> TryFrom<U> for T` in `core` once we implement `From<&'static str>`
            // below. Use the inherent `try_new` for fallible construction from borrowed strings.
            impl TryFrom<String> for $StructName {
                type Error = [<Invalid $StructName Error>];

                #[inline]
                fn try_from(string: String) -> Result<Self, Self::Error> {
                    Self::try_new(string)
                }
            }

            // Only `&'static str` (string literals / consts), so `impl Into<Self>` parameters stay
            // ergonomic for trusted compile-time values. Dynamic `&str`/`String` must go through
            // the fallible `try_new`/`TryFrom` instead.
            impl From<&'static str> for $StructName {
                /// # Panics
                /// Panics if `string` is empty.
                #[inline]
                fn from(string: &'static str) -> Self {
                    Self::from_static_str(string)
                }
            }

            impl AsRef<str> for $StructName {
                #[inline]
                fn as_ref(&self) -> &str {
                    self.as_str()
                }
            }

            impl std::ops::Deref for $StructName {
                type Target = str;

                #[inline]
                fn deref(&self) -> &str {
                    self.as_str()
                }
            }

            impl std::fmt::Debug for $StructName {
                #[inline]
                fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                    self.as_str().fmt(f)
                }
            }

            impl std::fmt::Display for $StructName {
                #[inline]
                fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                    self.as_str().fmt(f)
                }
            }

            impl<'a> PartialEq<&'a str> for $StructName {
                #[inline]
                fn eq(&self, other: &&'a str) -> bool {
                    self.as_str() == *other
                }
            }

            impl<'a> PartialEq<&'a str> for &$StructName {
                #[inline]
                fn eq(&self, other: &&'a str) -> bool {
                    self.as_str() == *other
                }
            }

            impl<'a> PartialEq<$StructName> for &'a str {
                #[inline]
                fn eq(&self, other: &$StructName) -> bool {
                    *self == other.as_str()
                }
            }

            impl re_byte_size::SizeBytes for $StructName {
                const IS_POD: bool = true;

                #[inline]
                fn heap_size_bytes(&self) -> u64 {
                    0
                }
            }

            impl $crate::external::serde::Serialize for $StructName {
                #[inline]
                fn serialize<S: $crate::external::serde::Serializer>(
                    &self,
                    serializer: S,
                ) -> Result<S::Ok, S::Error> {
                    $crate::external::serde::Serialize::serialize(self.as_str(), serializer)
                }
            }

            impl<'de> $crate::external::serde::Deserialize<'de> for $StructName {
                #[inline]
                fn deserialize<D: $crate::external::serde::Deserializer<'de>>(
                    deserializer: D,
                ) -> Result<Self, D::Error> {
                    use $crate::external::serde::de::Error as _;
                    let string = <String as $crate::external::serde::Deserialize>::deserialize(
                        deserializer,
                    )?;
                    Self::try_new(string).map_err(D::Error::custom)
                }
            }
        }
    };
}

// ----------------------------------------------------------------------------

use parking_lot::Mutex;
static GLOBAL_INTERNER: std::sync::LazyLock<Mutex<StringInterner>> =
    std::sync::LazyLock::new(|| Mutex::new(StringInterner::default()));

pub fn bytes_used() -> usize {
    GLOBAL_INTERNER.lock().bytes_used()
}

/// global interning function.
fn global_intern(string: &str) -> InternedString {
    GLOBAL_INTERNER.lock().intern(string)
}

// ----------------------------------------------------------------------------

#[test]
fn test_interner() {
    let mut interner = StringInterner::default();
    assert_eq!(interner.len(), 0);

    let a = interner.intern("Hello World!");
    assert_eq!(interner.len(), 1);

    let b = interner.intern("Hello World!");
    assert_eq!(interner.len(), 1);

    assert_eq!(a, b);

    let c = interner.intern("Another string");
    assert_eq!(interner.len(), 2);

    assert!(a.hash == b.hash);
    assert!(a.hash != c.hash);
}

#[test]
fn test_newtype_macro() {
    declare_new_type!(
        /// My typesafe string
        pub struct MyString;
    );
    let a = MyString::new("test");
    let b = MyString::new("test");
    assert_eq!(a, b);
    assert_eq!(a.as_str(), "test");
}

// This should never implement `Borrow`.
// See <https://github.com/rerun-io/rerun/pull/5243> for more information.
#[test]
fn do_not_implement_borrow() {
    declare_new_type!(
        /// My typesafe string
        pub struct MyString;
    );
    static_assertions::assert_not_impl_any!(MyString: std::borrow::Borrow<str>);
}

#[test]
fn test_nonempty_newtype_macro() {
    declare_new_type_nonempty!(
        /// My non-empty typesafe string
        pub struct MyNonEmptyString;
    );

    // Empty is rejected via the fallible entry points:
    assert!(MyNonEmptyString::try_new("").is_err());
    assert!(MyNonEmptyString::try_new(String::new()).is_err());
    assert!(MyNonEmptyString::try_from(String::new()).is_err());

    // Non-empty round-trips and interns:
    let a = MyNonEmptyString::try_new("test").expect("non-empty");
    let b = MyNonEmptyString::try_from("test".to_owned()).expect("non-empty");
    assert_eq!(a, b);
    assert_eq!(a.as_str(), "test");
    assert_eq!(a, "test");
    assert_eq!("test", a);

    // Trusted literal path:
    let c = MyNonEmptyString::from_static_str("test");
    assert_eq!(a, c);

    // `From<&'static str>` keeps `impl Into<_>` ergonomic:
    let d: MyNonEmptyString = "test".into();
    assert_eq!(a, d);

    fn takes(_: impl Into<MyNonEmptyString>) {}
    takes("test");

    // The error type is a real `std::error::Error` and reports why it was rejected:
    let err = MyNonEmptyString::try_new("").unwrap_err();
    let msg = std::string::ToString::to_string(&err);
    assert!(msg.contains("MyNonEmptyString"), "{msg:?}");
    assert!(msg.contains("must not be empty"), "{msg:?}");
    let _: &dyn std::error::Error = &err;
}

#[test]
#[should_panic(expected = "must not be empty")]
fn test_nonempty_from_static_str_panics_on_empty() {
    declare_new_type_nonempty!(
        /// My non-empty typesafe string
        pub struct MyNonEmptyString;
    );
    let _ = MyNonEmptyString::from_static_str("");
}

#[test]
#[should_panic(expected = "must not be empty")]
fn test_nonempty_from_empty_static_str_panics() {
    declare_new_type_nonempty!(
        /// My non-empty typesafe string
        pub struct MyNonEmptyString;
    );
    let _val: MyNonEmptyString = "".into();
}

// This should never implement `Borrow` (same as the plain macro).
#[test]
fn nonempty_do_not_implement_borrow() {
    declare_new_type_nonempty!(
        /// My non-empty typesafe string
        pub struct MyNonEmptyString;
    );
    static_assertions::assert_not_impl_any!(MyNonEmptyString: std::borrow::Borrow<str>);
}