moos 0.3.0

Memory-Optimized Objects and Strings (MOOS)
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
use alloc::borrow::Borrow;
use alloc::borrow::BorrowMut;
use alloc::borrow::Cow;
use alloc::borrow::ToOwned;
use alloc::string::String;
use alloc::string::ToString;
use core::cmp::Ordering;
use core::convert::AsMut;
use core::convert::AsRef;
use core::convert::From;
use core::convert::TryFrom;
use core::fmt;
use core::fmt::Debug;
use core::fmt::Display;
use core::fmt::Formatter;
use core::hash::Hash;
use core::hash::Hasher;
use core::mem::size_of;
use core::ops::Deref;
use core::ops::DerefMut;
use core::str;
use core::str::FromStr;

use crate::CowStr;

/// Maximum length of an inline string in bytes. On 64-bit systems this is
/// typically 22 bytes, while on 32-bit systems, it's usually only 10 bytes.
///
/// This value is calculated as 3 times the size of an `isize` (to account for
/// UTF-8 encoding), **minus 2 bytes** to reserve space for a `u8` length byte
/// and a null terminator (`\0`) character (not stored but conceptually present
/// in a manner similar to C-style strings).
pub const MAX_INLINE_STR_LEN: usize = 3 * size_of::<isize>() - 2;

/// Error type returned when attempting to create an `InlineStr` from a string
/// or `&str` reference that exceeds the maximum allowed length determined by
/// the [`MAX_INLINE_STR_LEN`] constant.
///
/// # Example
///
/// ```rust
/// # use moos::inline_str::*;
/// # use core::convert::TryFrom;
/// # fn main() {
/// let long_str = "This string is too long to fit in an InlineStr";
/// let result = InlineStr::try_from(long_str);
///
/// assert!(result.is_err());
/// assert!(matches!(result, Err(StringTooLongError)));
///
/// # }
/// ```
#[derive(Debug, Clone, Copy)]
pub struct StringTooLongError;

#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "constructors", derive(derive_more::Constructor))]
#[cfg_attr(
  feature = "index",
  derive(derive_more::Index, derive_more::IndexMut)
)]
/// Represents a short inline string stored on the stack in fixed-size buffers.
///
/// Designed to hold very short strings (up to [`MAX_INLINE_STR_LEN`] bytes),
/// this type is useful for optimizing memory usage in scenarios where you
/// expect to frequently work with small strings.
///
/// Attempting to store a string longer than the maximum length will result in
/// a [`StringTooLongError`] being returned.
///
/// # Example
///
/// ```rust
/// # use moos::inline_str::*;
/// # use core::convert::TryFrom;
///
/// # fn main() -> Result<(), StringTooLongError> {
/// let inline_str: InlineStr = "Hello".parse()?;
/// assert_eq!(inline_str.as_ref(), "Hello");
/// assert_eq!(inline_str.len(), 5);
///
/// // This will fail because the string is too long:
/// let long_str = "This string is too long to fit in an InlineStr";
/// let result = InlineStr::try_from(long_str);
/// assert!(result.is_err());
/// assert!(matches!(result, Err(StringTooLongError)));
///
/// # Ok(())
/// # }
/// ```
pub struct InlineStr {
  #[cfg_attr(feature = "index", index)]
  #[cfg_attr(feature = "index", index_mut)]
  pub(crate) buf: [u8; MAX_INLINE_STR_LEN],
  pub(crate) len: u8,
}

impl InlineStr {
  /// Creates a new `InlineStr`.
  #[cfg(not(feature = "constructors"))]
  pub const fn new(buf: [u8; MAX_INLINE_STR_LEN], len: u8) -> Self {
    Self { buf, len }
  }

  /// Returns the length of the string.
  #[inline]
  pub const fn len(&self) -> usize {
    self.len as usize
  }

  /// Returns whether the string is empty.
  #[inline]
  pub const fn is_empty(&self) -> bool {
    self.len == 0
  }

  /// Returns a reference to the underlying byte buffer.
  #[inline]
  pub fn as_bytes(&self) -> &[u8] {
    &self.buf[..self.len as usize]
  }

  /// Returns a mutable reference to the underlying byte buffer.
  #[inline]
  pub fn as_bytes_mut(&mut self) -> &mut [u8] {
    &mut self.buf[..self.len as usize]
  }

  /// Returns a reference to the string as a slice.
  ///
  /// # Panics
  ///
  /// This method panics if the internal byte buffer does not contain valid
  /// UTF-8 data.
  #[inline]
  pub fn as_str(&self) -> &str {
    if let Ok(s) = str::from_utf8(self.as_bytes()) {
      s
    } else {
      panic!("InlineStr should only contain valid UTF-8 data");
    }
  }

  /// Returns a mutable reference to the string as a slice.
  #[inline]
  pub fn as_mut_str(&mut self) -> Result<&mut str, str::Utf8Error> {
    str::from_utf8_mut(self.as_bytes_mut())
  }

  /// Returns a reference to the string as a slice, without checking
  /// for UTF-8 validity.
  ///
  /// # Safety
  ///
  /// The caller must ensure the data is valid UTF-8.
  #[inline]
  pub unsafe fn as_str_unchecked(&self) -> &str {
    unsafe { str::from_utf8_unchecked(self.as_bytes()) }
  }

  /// Returns a mutable reference to the string as a slice, without checking
  /// for UTF-8 validity.
  ///
  /// # Safety
  ///
  /// The caller must ensure the data is valid UTF-8.
  #[inline]
  pub unsafe fn as_mut_str_unchecked(&mut self) -> &mut str {
    unsafe { str::from_utf8_unchecked_mut(self.as_bytes_mut()) }
  }
}

impl Default for InlineStr {
  #[inline(always)]
  fn default() -> Self {
    Self {
      buf: [0u8; MAX_INLINE_STR_LEN],
      len: 0,
    }
  }
}

impl Display for InlineStr {
  #[inline(always)]
  fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
    write!(f, "{}", self.as_str())
  }
}

impl Borrow<str> for InlineStr {
  #[inline(always)]
  fn borrow(&self) -> &str {
    self.as_ref()
  }
}

impl BorrowMut<str> for InlineStr {
  #[inline(always)]
  fn borrow_mut(&mut self) -> &mut str {
    self.as_mut_str().unwrap_or_default()
  }
}

impl Deref for InlineStr {
  type Target = str;

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

impl DerefMut for InlineStr {
  #[inline(always)]
  fn deref_mut(&mut self) -> &mut str {
    self.as_mut_str().unwrap_or_default()
  }
}

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

impl AsMut<str> for InlineStr {
  #[inline(always)]
  fn as_mut(&mut self) -> &mut str {
    self.deref_mut()
  }
}

impl From<InlineStr> for String {
  #[inline(always)]
  fn from(s: InlineStr) -> Self {
    s.deref().to_owned()
  }
}

impl From<&InlineStr> for String {
  #[inline(always)]
  fn from(s: &InlineStr) -> Self {
    s.deref().to_owned()
  }
}

impl<T: AsRef<str>> From<&T> for InlineStr {
  #[inline(always)]
  fn from(s: &T) -> Self {
    InlineStr::try_from(s.as_ref())
      .expect("String length exceeds InlineStr maximum capacity")
  }
}

impl From<char> for InlineStr {
  #[inline(always)]
  fn from(c: char) -> Self {
    let mut buf = [0u8; MAX_INLINE_STR_LEN];
    c.encode_utf8(&mut buf);
    let len = c.len_utf8() as u8;
    Self { buf, len }
  }
}

impl<'i> From<Cow<'i, str>> for InlineStr {
  #[inline(always)]
  fn from(cow: Cow<'i, str>) -> Self {
    let src = cow.as_ref().as_bytes();
    let len = src.len().min(MAX_INLINE_STR_LEN);
    let mut buf = [0u8; MAX_INLINE_STR_LEN];
    buf[..len].copy_from_slice(&src[..len]);
    let len = len as u8;
    Self { buf, len }
  }
}

impl FromStr for InlineStr {
  type Err = StringTooLongError;

  #[inline(always)]
  fn from_str(s: &str) -> Result<InlineStr, StringTooLongError> {
    InlineStr::try_from(s)
  }
}

impl From<String> for InlineStr {
  #[inline(always)]
  fn from(s: String) -> Self {
    let src = s.as_bytes();
    let len = src.len().min(MAX_INLINE_STR_LEN);
    let mut buf = [0u8; MAX_INLINE_STR_LEN];
    buf[..len].copy_from_slice(&src[..len]);
    let len = len as u8;
    Self { buf, len }
  }
}

impl TryFrom<&str> for InlineStr {
  type Error = StringTooLongError;

  #[inline(always)]
  fn try_from(s: &str) -> Result<InlineStr, StringTooLongError> {
    let len = s.len();
    if len > MAX_INLINE_STR_LEN {
      return Err(StringTooLongError);
    }
    let mut buf = [0u8; MAX_INLINE_STR_LEN];
    buf[..len].copy_from_slice(s.as_bytes());
    let len = len as u8;
    Ok(Self { buf, len })
  }
}

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

impl<T: ToString> PartialEq<T> for InlineStr {
  #[inline(always)]
  fn eq(&self, other: &T) -> bool {
    self.deref() == other.to_string()
  }
}

impl PartialEq<InlineStr> for &InlineStr {
  #[inline(always)]
  fn eq(&self, other: &InlineStr) -> bool {
    **self == *other
  }
}

impl PartialEq<str> for InlineStr {
  #[inline(always)]
  fn eq(&self, other: &str) -> bool {
    self.deref() == other
  }
}

impl<'i> PartialEq<InlineStr> for Cow<'i, str> {
  #[inline(always)]
  fn eq(&self, other: &InlineStr) -> bool {
    self.deref() == other.deref()
  }
}

impl<'i> PartialEq<InlineStr> for CowStr<'i> {
  #[inline(always)]
  fn eq(&self, other: &InlineStr) -> bool {
    self.deref() == other.deref()
  }
}

impl PartialEq<InlineStr> for &str {
  #[inline(always)]
  fn eq(&self, other: &InlineStr) -> bool {
    *self == other.deref()
  }
}

impl PartialEq<InlineStr> for str {
  #[inline(always)]
  fn eq(&self, other: &InlineStr) -> bool {
    self == other.deref()
  }
}

impl PartialEq<InlineStr> for char {
  #[inline(always)]
  fn eq(&self, other: &InlineStr) -> bool {
    let other_str = other.deref();
    if let Some(first_char) = other_str.chars().next() {
      first_char == *self && other_str.len() == self.len_utf8()
    } else {
      false
    }
  }
}

impl PartialEq<InlineStr> for String {
  #[inline(always)]
  fn eq(&self, other: &InlineStr) -> bool {
    self.as_str() == other.deref()
  }
}

impl PartialEq<InlineStr> for &String {
  #[inline(always)]
  fn eq(&self, other: &InlineStr) -> bool {
    self.as_str() == other.deref()
  }
}

impl PartialEq<InlineStr> for &&str {
  #[inline(always)]
  fn eq(&self, other: &InlineStr) -> bool {
    **self == other.deref()
  }
}

impl PartialEq<InlineStr> for &mut str {
  #[inline(always)]
  fn eq(&self, other: &InlineStr) -> bool {
    &**self == other.deref()
  }
}

impl PartialEq<InlineStr> for &mut String {
  #[inline(always)]
  fn eq(&self, other: &InlineStr) -> bool {
    self.as_str() == other.deref()
  }
}

impl PartialEq<InlineStr> for &mut InlineStr {
  #[inline(always)]
  fn eq(&self, other: &InlineStr) -> bool {
    **self == *other
  }
}

impl Eq for InlineStr {}

impl PartialOrd<str> for InlineStr {
  #[inline(always)]
  fn partial_cmp(&self, other: &str) -> Option<Ordering> {
    Some(self.deref().cmp(other))
  }
}

impl PartialOrd<InlineStr> for str {
  #[inline(always)]
  fn partial_cmp(&self, other: &InlineStr) -> Option<Ordering> {
    Some(self.cmp(other.deref()))
  }
}

impl PartialOrd<InlineStr> for char {
  #[inline(always)]
  fn partial_cmp(&self, other: &InlineStr) -> Option<Ordering> {
    let that = other.deref();
    if let Some(first_char) = that.chars().next() {
      Some(self.cmp(&first_char))
    } else {
      Some(Ordering::Greater)
    }
  }
}

impl PartialOrd<InlineStr> for String {
  fn partial_cmp(&self, other: &InlineStr) -> Option<Ordering> {
    Some(self.as_str().cmp(other.deref()))
  }
}

impl PartialOrd<InlineStr> for &String {
  #[inline(always)]
  fn partial_cmp(&self, other: &InlineStr) -> Option<Ordering> {
    Some(self.as_str().cmp(other.deref()))
  }
}

impl PartialOrd<InlineStr> for &&str {
  #[inline(always)]
  fn partial_cmp(&self, other: &InlineStr) -> Option<Ordering> {
    Some((**self).cmp(other.deref()))
  }
}

impl PartialOrd<InlineStr> for &mut str {
  #[inline(always)]
  fn partial_cmp(&self, other: &InlineStr) -> Option<Ordering> {
    Some((**self).cmp(other.deref()))
  }
}

impl PartialOrd<InlineStr> for &mut String {
  #[inline(always)]
  fn partial_cmp(&self, other: &InlineStr) -> Option<Ordering> {
    Some(self.as_str().cmp(other.deref()))
  }
}

impl PartialOrd<InlineStr> for &mut InlineStr {
  #[inline(always)]
  fn partial_cmp(&self, other: &InlineStr) -> Option<Ordering> {
    Some((**self).deref().cmp(other.deref()))
  }
}

impl<'i> PartialOrd<InlineStr> for Cow<'i, str> {
  #[inline(always)]
  fn partial_cmp(&self, other: &InlineStr) -> Option<Ordering> {
    Some(self.deref().cmp(other.deref()))
  }
}

impl<'i> PartialOrd<InlineStr> for CowStr<'i> {
  #[inline(always)]
  fn partial_cmp(&self, other: &InlineStr) -> Option<Ordering> {
    Some(self.deref().cmp(other.deref()))
  }
}

impl<T: ToString> PartialOrd<T> for InlineStr {
  #[inline(always)]
  fn partial_cmp(&self, other: &T) -> Option<Ordering> {
    let that = other.to_string();
    Some(self.deref().cmp(&that))
  }
}

impl Ord for InlineStr {
  #[inline(always)]
  fn cmp(&self, other: &Self) -> Ordering {
    self.deref().cmp(other.deref())
  }
}

#[cfg(test)]
mod tests {
  use super::*;

  #[test]
  fn max_inline_str_len_is_at_least_4_bytes() {
    let max = MAX_INLINE_STR_LEN;
    assert!(max >= 4);
  }

  #[test]
  fn inline_str_from_ascii_char() {
    let s: InlineStr = 'a'.into();
    assert_eq!("a", s.deref());
  }

  #[test]
  fn inline_str_from_unicode_char() {
    let s: InlineStr = '🍔'.into();
    assert_eq!("🍔", s.deref());
  }

  #[test]
  #[cfg(target_pointer_width = "64")]
  fn inline_str_fits_twentytwo() {
    let s = "0123456789abcdefghijkl";
    let stack_str = InlineStr::try_from(s);
    assert!(stack_str.is_ok());
    let stack_str = stack_str.unwrap();
    assert_eq!(stack_str.len(), 22);
    assert_eq!(stack_str.deref().len(), 22);
    assert_eq!(stack_str.deref(), s);
  }

  #[test]
  #[cfg(target_pointer_width = "64")]
  fn inline_str_not_fits_twentythree() {
    let s = "0123456789abcdefghijklm";
    let err = InlineStr::try_from(s);
    assert!(err.is_err());
    assert!(matches!(err, Err(StringTooLongError)));
  }

  #[test]
  #[cfg(target_pointer_width = "64")]
  fn try_inline_str_from_str() {
    let s = "Hello, world!";
    let inline_str = InlineStr::try_from(s);
    assert!(inline_str.is_ok());
    let inline_str = inline_str.unwrap();
    assert_eq!(inline_str.deref(), s);
  }

  #[test]
  #[cfg(target_pointer_width = "32")]
  fn inline_str_fits_ten() {
    let s = "0123456789";
    let stack_str = InlineStr::try_from(s);
    assert!(stack_str.is_ok());
    let stack_str = stack_str.unwrap();
    assert_eq!(stack_str.len(), 10);
    assert_eq!(stack_str.deref().len(), 10);
    assert_eq!(stack_str.deref(), s);
  }

  #[test]
  #[cfg(target_pointer_width = "32")]
  fn inline_str_not_fits_eleven() {
    let s = "0123456789a";
    let err = InlineStr::try_from(s);
    assert!(err.is_err());
    assert!(matches!(err, Err(StringTooLongError)));
  }

  #[test]
  fn try_inline_str_from_long_str() {
    let s = "This string is too long to fit in an InlineStr";
    let err = InlineStr::try_from(s);
    assert!(err.is_err());
    assert!(matches!(err, Err(StringTooLongError)));
  }

  #[test]
  fn inline_str_equality() {
    let s1: InlineStr = "Hello".try_into().unwrap();
    let s2: InlineStr = "Hello".try_into().unwrap();
    let s3: InlineStr = "World".try_into().unwrap();
    assert_eq!(s1, s2);
    assert_ne!(s1, s3);
    assert!(s1 < s3);
    assert!(s2 <= s1);
    assert!(s3 > s1);
  }

  #[test]
  fn inline_str_char_equality() {
    let s: InlineStr = "A".try_into().unwrap();
    let c: char = 'A';
    assert_eq!(s, c);
    assert_eq!(c, s);
  }

  #[test]
  fn inline_str_cow_equality() {
    let s: InlineStr = "Hello".try_into().unwrap();
    let cow: Cow<str> = Cow::Borrowed("Hello");
    assert_eq!(s, cow);
    assert_eq!(cow, s);
  }

  #[test]
  fn inline_str_as_mut_str() {
    let mut s: InlineStr = "Hello".try_into().unwrap();
    {
      let s_mut = s.as_mut_str().unwrap();
      s_mut.make_ascii_uppercase();
      assert_eq!(s_mut, "HELLO");
    }
    assert_eq!(s, "HELLO");
  }
}