ref_str 0.2.0

Compressed borrowed-or-shared string types for no_std Rust.
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
//! Internal core implementation for compressed string types.
//!
//! This module contains the backend-agnostic compact string engine. It is the
//! layer that decides when a value stays borrowed, becomes inline, or is stored
//! in shared heap-backed form.

pub use crate::backend::{LocalBackend, RefCountBackend, SharedBackend};

use ::core::borrow::Borrow;
use ::core::cmp::Ordering;
use ::core::fmt;
use ::core::hash::{Hash, Hasher};
use ::core::marker::PhantomData;
use ::core::mem::ManuallyDrop;
use ::core::ops::Deref;
use ::core::ptr;
use alloc::borrow::Cow;
use alloc::boxed::Box;
use alloc::rc::Rc;
use alloc::string::{String, ToString};
use alloc::sync::Arc;
use alloc::vec::Vec;

use crate::RawParts;
use crate::arch::encode;
use crate::arch::layout;
use crate::arch::layout::StateTag;

/// Internal compact two-word string representation.
///
/// The payload itself is only two machine words wide. The type parameters keep
/// the borrowed lifetime and the backend choice visible to the compiler.
#[repr(C)]
pub struct RefStrCore<'a, B: RefCountBackend> {
    /// Compact raw representation.
    parts: RawParts,
    /// Carries the borrowed lifetime.
    _marker: PhantomData<&'a str>,
    /// Keeps the backend type in the type system.
    _backend: PhantomData<B>,
}

unsafe impl<'a, B> Send for RefStrCore<'a, B>
where
    B: RefCountBackend,
    B::Shared: Send,
{
}

unsafe impl<'a, B> Sync for RefStrCore<'a, B>
where
    B: RefCountBackend,
    B::Shared: Sync,
{
}

impl<'a, B: RefCountBackend> RefStrCore<'a, B> {
    /// Construct from a borrowed string.
    #[inline]
    pub const fn new(s: &'a str) -> Self {
        Self::from_str(s)
    }

    /// Construct from a borrowed string and cache state derived from it.
    #[inline]
    pub const fn from_str(s: &'a str) -> Self {
        let raw_ptr = s.as_ptr();
        let hash = encode::short_hash(s.as_bytes());
        let mut meta = encode::encode_len_tag_hash(s.len(), StateTag::Borrowed, hash);
        if s.is_ascii() {
            meta |= layout::IS_ASCII_MASK;
        }

        unsafe { Self::from_raw_parts_struct(RawParts::new(raw_ptr, meta)) }
    }

    /// Construct from any string-like input, preferring inline storage.
    #[inline]
    pub fn from_owned_like<R: AsRef<str>>(s: R) -> Self {
        let s = s.as_ref();

        if encode::supports_inline_len(s.len()) {
            Self::new_inline(s)
        } else {
            Self::from_shared(B::from_str(s))
        }
    }

    /// Rebuild a core value from raw parts.
    ///
    /// # Safety
    ///
    /// The `parts` value must originate from this crate and match the backend
    /// and lifetime contract of the target type.
    #[inline]
    pub const unsafe fn from_raw_parts(parts: RawParts) -> Self {
        unsafe { Self::from_raw_parts_struct(parts) }
    }

    #[inline]
    pub(crate) const unsafe fn from_raw_parts_struct(parts: RawParts) -> Self {
        Self {
            parts,
            _marker: PhantomData,
            _backend: PhantomData,
        }
    }

    /// Construct from backend-owned shared storage.
    pub fn from_shared(s: B::Shared) -> Self {
        let len = s.deref().len();
        let is_ascii = s.deref().is_ascii();
        let hash = encode::short_hash(s.deref().as_bytes());
        let raw = B::into_raw(s);
        let raw_ptr = raw as *const u8;
        let mut meta = encode::encode_len_tag_hash(len, StateTag::Shared, hash);
        if is_ascii {
            meta |= layout::IS_ASCII_MASK;
        }

        unsafe { Self::from_raw_parts_struct(RawParts::new(raw_ptr, meta)) }
    }

    /// Construct from an owned `String`, using inline storage when possible.
    fn from_owned_string(value: String) -> Self {
        if encode::supports_inline_len(value.len()) {
            Self::new_inline(value.as_str())
        } else {
            Self::from_shared(B::from_string(value))
        }
    }

    /// Construct from an owned `Box<str>`, using inline storage when possible.
    fn from_owned_boxed_str(value: Box<str>) -> Self {
        if encode::supports_inline_len(value.len()) {
            Self::new_inline(value.as_ref())
        } else {
            Self::from_shared(B::from_boxed_str(value))
        }
    }

    /// Construct an inline string value.
    fn new_inline(s: &str) -> Self {
        unsafe { Self::from_raw_parts_struct(RawParts::pack_inline(s)) }
    }

    /// Decompose the value into its raw transport representation.
    ///
    /// # Safety
    ///
    /// Any shared payload's ownership responsibility moves to the caller.
    pub const unsafe fn into_raw_parts(self) -> RawParts {
        unsafe { self.into_raw_parts_struct() }
    }

    pub(crate) const unsafe fn into_raw_parts_struct(self) -> RawParts {
        #[repr(C)]
        union View<'a, B: RefCountBackend> {
            core: ManuallyDrop<RefStrCore<'a, B>>,
            parts: RawParts,
        }

        let view = View {
            core: ManuallyDrop::new(self),
        };

        unsafe { view.parts }
    }

    /// Convert into a raw `*const str`.
    ///
    /// Inline values are first promoted to shared storage.
    ///
    /// # Safety
    ///
    /// The result may point to borrowed data or shared backend storage.
    pub unsafe fn into_raw(self) -> *const str {
        let parts = self.parts;
        if parts.is_inline() {
            B::into_raw(B::from_str(self.as_str()))
        } else {
            unsafe { self.into_raw_parts_struct().into_raw_non_inline() }
        }
    }

    /// Convert into a raw pointer only if the payload is already shared.
    pub fn into_raw_shared(self) -> Option<*const str> {
        let parts = self.parts;
        if parts.is_shared() {
            Some(unsafe { self.into_raw_parts_struct().into_raw_non_inline() })
        } else {
            None
        }
    }

    /// Increment the strong count for a backend-owned shared pointer.
    ///
    /// # Safety
    ///
    /// `ptr` must come from the same backend and still reference a live
    /// allocation.
    pub unsafe fn increment_strong_count(ptr: *const str) {
        unsafe {
            B::increment_strong_count(ptr);
        }
    }

    #[inline]
    const fn state_tag(&self) -> StateTag {
        self.parts.tag()
    }

    #[inline]
    pub const fn is_shared(&self) -> bool {
        self.parts.is_shared()
    }

    #[inline]
    pub const fn is_borrowed(&self) -> bool {
        self.parts.is_borrowed()
    }

    #[inline]
    pub const fn is_inline(&self) -> bool {
        self.parts.is_inline()
    }

    #[inline]
    pub const fn is_ascii(&self) -> bool {
        self.parts.is_ascii()
    }

    #[inline]
    pub const fn len(&self) -> usize {
        self.parts.len()
    }

    #[inline]
    pub const fn is_empty(&self) -> bool {
        self.len() == 0
    }

    #[inline]
    /// Borrow the contents as `&str`.
    pub fn as_str(&self) -> &str {
        unsafe {
            let parts = self.parts;
            let meta = parts.meta();
            let (raw_ptr, len) = if (meta & layout::INLINE_MASK) != 0 {
                (
                    self as *const Self as *const u8,
                    encode::inline_len_from_meta(meta),
                )
            } else {
                (parts.as_ptr(), encode::decode_borrowed_or_shared_len(meta))
            };
            let slice = ::core::slice::from_raw_parts(raw_ptr, len);
            ::core::str::from_utf8_unchecked(slice)
        }
    }

    /// Convert a possibly borrowed value into an owned `'static` core.
    pub fn to_static_core(&self) -> RefStrCore<'static, B> {
        match self.state_tag() {
            StateTag::Shared => {
                let cloned = self.clone();
                let parts = unsafe { cloned.into_raw_parts() };
                unsafe { RefStrCore::from_raw_parts(parts) }
            }
            StateTag::Inline => unsafe {
                RefStrCore::from_raw_parts(self.clone().into_raw_parts())
            },
            StateTag::Borrowed => {
                if encode::supports_inline_len(self.len()) {
                    RefStrCore::new_inline(self.as_str())
                } else {
                    RefStrCore::from_shared(B::from_str(self.as_str()))
                }
            }
        }
    }

    /// Convert this value into an owned `'static` core.
    pub fn into_static_core(self) -> RefStrCore<'static, B> {
        match self.state_tag() {
            StateTag::Shared | StateTag::Inline => {
                let parts = unsafe { self.into_raw_parts() };
                unsafe { RefStrCore::from_raw_parts(parts) }
            }
            StateTag::Borrowed => {
                if encode::supports_inline_len(self.len()) {
                    RefStrCore::new_inline(self.as_str())
                } else {
                    RefStrCore::from_shared(B::from_str(self.as_str()))
                }
            }
        }
    }

    /// Consume the value and return its UTF-8 bytes.
    pub fn into_bytes(self) -> Vec<u8> {
        let parts = self.parts;
        let len = parts.len();
        let ptr = if parts.is_inline() {
            &self as *const Self as *const u8
        } else {
            parts.as_ptr()
        };

        unsafe { ::core::slice::from_raw_parts(ptr, len).to_vec() }
    }

    /// Consume the value and return `Box<str>`.
    pub fn into_boxed_str(self) -> Box<str> {
        Box::from(self.as_str())
    }

    /// Consume the value and return `String`.
    pub fn into_string(self) -> String {
        String::from(self.as_str())
    }

    /// Consume the value and return `Cow<str>`.
    pub fn into_cow(self) -> Cow<'a, str> {
        if self.is_borrowed() {
            unsafe { Cow::Borrowed(self.into_str_unchecked()) }
        } else {
            Cow::Owned(self.into_string())
        }
    }

    #[inline]
    const unsafe fn borrowed_str_unchecked(&self) -> &'a str {
        let parts = self.parts;
        let meta = parts.meta();
        let slice =
            ptr::slice_from_raw_parts(parts.as_ptr(), encode::decode_borrowed_or_shared_len(meta))
                as *const str;

        unsafe { &*slice }
    }

    /// Convert into `&'a str` without checking the state tag.
    ///
    /// # Safety
    ///
    /// The value must currently store a borrowed string.
    pub const unsafe fn into_str_unchecked(self) -> &'a str {
        debug_assert!(
            self.is_borrowed(),
            "into_str_unchecked requires a borrowed value"
        );

        let this = ManuallyDrop::new(self);
        let this_ptr = &this as *const ManuallyDrop<Self> as *const Self;

        unsafe { (&*this_ptr).borrowed_str_unchecked() }
    }

    /// Borrow as `Cow<str>`, avoiding allocation for borrowed values.
    pub fn as_cow(&self) -> Cow<'a, str> {
        if self.is_borrowed() {
            unsafe { Cow::Borrowed(self.borrowed_str_unchecked()) }
        } else {
            Cow::Owned(self.to_string())
        }
    }
}

impl<B: RefCountBackend> RefStrCore<'static, B> {
    /// Construct a `'static` core from a borrowed `'static` string.
    pub fn from_static(s: &'static str) -> Self {
        Self::from_str(s)
    }

    /// Return the borrowed `'static` string if this value is borrowed.
    pub fn borrowed_static_str(&self) -> Option<&'static str> {
        let parts = self.parts;
        if parts.is_borrowed() {
            Some(unsafe { self.borrowed_str_unchecked() })
        } else {
            None
        }
    }
}

impl<'a, B: RefCountBackend> From<&'a str> for RefStrCore<'a, B> {
    fn from(value: &'a str) -> Self {
        Self::new(value)
    }
}

impl<'a, B: RefCountBackend> From<&'a String> for RefStrCore<'a, B> {
    fn from(value: &'a String) -> Self {
        Self::from(value.as_str())
    }
}

impl<'a> From<Rc<str>> for RefStrCore<'a, LocalBackend> {
    fn from(value: Rc<str>) -> Self {
        Self::from_shared(value)
    }
}

impl<'a> From<Arc<str>> for RefStrCore<'a, SharedBackend> {
    fn from(value: Arc<str>) -> Self {
        Self::from_shared(value)
    }
}

impl<'a> From<RefStrCore<'a, LocalBackend>> for RefStrCore<'a, SharedBackend> {
    fn from(value: RefStrCore<'a, LocalBackend>) -> Self {
        if value.is_shared() {
            Self::from_shared(Arc::from(value.as_str()))
        } else {
            let parts = unsafe { value.into_raw_parts() };
            unsafe { Self::from_raw_parts(parts) }
        }
    }
}

impl<'a> From<RefStrCore<'a, SharedBackend>> for RefStrCore<'a, LocalBackend> {
    fn from(value: RefStrCore<'a, SharedBackend>) -> Self {
        if value.is_shared() {
            Self::from_shared(Rc::from(value.as_str()))
        } else {
            let parts = unsafe { value.into_raw_parts() };
            unsafe { Self::from_raw_parts(parts) }
        }
    }
}

impl<'a, B: RefCountBackend> From<Box<str>> for RefStrCore<'a, B> {
    fn from(value: Box<str>) -> Self {
        Self::from_owned_boxed_str(value)
    }
}

impl<'a, B: RefCountBackend> From<String> for RefStrCore<'a, B> {
    fn from(value: String) -> Self {
        Self::from_owned_string(value)
    }
}

impl<'a, B: RefCountBackend> From<RefStrCore<'a, B>> for Cow<'a, str> {
    fn from(value: RefStrCore<'a, B>) -> Self {
        value.into_cow()
    }
}

impl<'a, B: RefCountBackend> From<Cow<'a, str>> for RefStrCore<'a, B> {
    fn from(value: Cow<'a, str>) -> Self {
        match value {
            Cow::Borrowed(s) => Self::from(s),
            Cow::Owned(s) => Self::from(s),
        }
    }
}

impl<'a, B: RefCountBackend> Default for RefStrCore<'a, B> {
    fn default() -> Self {
        Self::from_str("")
    }
}

impl<'a, B: RefCountBackend> AsRef<str> for RefStrCore<'a, B> {
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

impl<'a, B: RefCountBackend> Borrow<str> for RefStrCore<'a, B> {
    fn borrow(&self) -> &str {
        self.as_str()
    }
}

impl<'a, B: RefCountBackend> PartialEq for RefStrCore<'a, B> {
    fn eq(&self, other: &Self) -> bool {
        if self.parts.raw_ptr() == other.parts.raw_ptr() && self.parts.meta() == other.parts.meta()
        {
            return true;
        }

        if self.is_inline() || other.is_inline() {
            return self.as_str() == other.as_str();
        }

        if self.parts.cached_hash() != other.parts.cached_hash() {
            return false;
        }

        if self.len() != other.len() {
            return false;
        }

        self.as_str() == other.as_str()
    }
}

impl<'a, B: RefCountBackend> Eq for RefStrCore<'a, B> {}

impl<'a, B: RefCountBackend> PartialEq<&str> for RefStrCore<'a, B> {
    fn eq(&self, other: &&str) -> bool {
        self.as_str() == *other
    }
}

impl<'a, B: RefCountBackend> PartialEq<String> for RefStrCore<'a, B> {
    fn eq(&self, other: &String) -> bool {
        self.as_str() == other.as_str()
    }
}

impl<'a, B: RefCountBackend> PartialOrd for RefStrCore<'a, B> {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl<'a, B: RefCountBackend> Ord for RefStrCore<'a, B> {
    fn cmp(&self, other: &Self) -> Ordering {
        self.as_str().cmp(other.as_str())
    }
}

impl<'a, B: RefCountBackend> Hash for RefStrCore<'a, B> {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.as_str().hash(state)
    }
}

impl<'a, B: RefCountBackend> Deref for RefStrCore<'a, B> {
    type Target = str;

    fn deref(&self) -> &Self::Target {
        self.as_str()
    }
}

impl<'a, B: RefCountBackend> fmt::Debug for RefStrCore<'a, B> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if f.alternate() {
            let state = match self.state_tag() {
                StateTag::Borrowed => "Borrowed",
                StateTag::Shared => "Shared",
                StateTag::Inline => "Inline",
            };

            f.debug_struct("RefStrCore")
                .field("state", &state)
                .field("len", &self.len())
                .field("value", &self.as_str())
                .finish()
        } else {
            f.debug_tuple("RefStrCore").field(&self.as_str()).finish()
        }
    }
}

impl<'a, B: RefCountBackend> fmt::Display for RefStrCore<'a, B> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

impl<'a, B: RefCountBackend> Clone for RefStrCore<'a, B> {
    fn clone(&self) -> Self {
        let parts = self.parts;
        let meta = parts.meta();
        if (meta & layout::NEEDS_DROP_MASK) != 0 {
            let len = encode::decode_borrowed_or_shared_len(meta);
            let fat_ptr = ptr::slice_from_raw_parts(parts.as_ptr(), len) as *const str;
            unsafe {
                B::increment_strong_count(fat_ptr);
            }
        }

        Self {
            parts,
            _marker: PhantomData,
            _backend: PhantomData,
        }
    }
}

impl<'a, B: RefCountBackend> Drop for RefStrCore<'a, B> {
    fn drop(&mut self) {
        let parts = self.parts;
        let meta = parts.meta();
        if (meta & layout::NEEDS_DROP_MASK) != 0 {
            let len = encode::decode_borrowed_or_shared_len(meta);
            let fat_ptr = ptr::slice_from_raw_parts(parts.as_ptr(), len) as *const str;
            unsafe {
                drop(B::from_raw(fat_ptr));
            }
        }
    }
}