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
use crate as stabby;
use core::ops::{Deref, DerefMut};
#[stabby::stabby]
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct Str<'a> {
pub(crate) inner: crate::slice::Slice<'a, u8>,
}
impl<'a> Str<'a> {
pub const fn new(s: &'a str) -> Self {
Self {
inner: crate::slice::Slice::new(s.as_bytes()),
}
}
pub const fn as_str(self) -> &'a str {
unsafe { core::str::from_utf8_unchecked(self.inner.as_slice()) }
}
}
impl<'a> From<&'a str> for Str<'a> {
fn from(value: &'a str) -> Self {
Self::new(value)
}
}
impl<'a> From<&'a mut str> for Str<'a> {
fn from(value: &'a mut str) -> Self {
Self::from(&*value)
}
}
impl<'a> From<Str<'a>> for &'a str {
fn from(value: Str<'a>) -> Self {
unsafe { core::str::from_utf8_unchecked(value.inner.into()) }
}
}
impl<'a> Deref for Str<'a> {
type Target = str;
fn deref(&self) -> &Self::Target {
unsafe { core::str::from_utf8_unchecked(&self.inner) }
}
}
impl core::fmt::Debug for Str<'_> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
self.deref().fmt(f)
}
}
impl core::fmt::Display for Str<'_> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
self.deref().fmt(f)
}
}
impl core::cmp::PartialOrd for Str<'_> {
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
self.deref().partial_cmp(other.deref())
}
}
#[stabby::stabby]
pub struct StrMut<'a> {
pub(crate) inner: crate::slice::SliceMut<'a, u8>,
}
impl<'a> Deref for StrMut<'a> {
type Target = str;
fn deref(&self) -> &Self::Target {
unsafe { core::str::from_utf8_unchecked(&self.inner) }
}
}
impl<'a> DerefMut for StrMut<'a> {
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe { core::str::from_utf8_unchecked_mut(&mut self.inner) }
}
}
impl<'a> From<&'a mut str> for StrMut<'a> {
fn from(value: &'a mut str) -> Self {
Self {
inner: unsafe { value.as_bytes_mut().into() },
}
}
}
impl<'a> From<StrMut<'a>> for Str<'a> {
fn from(value: StrMut<'a>) -> Self {
Self {
inner: value.inner.into(),
}
}
}
impl<'a> From<StrMut<'a>> for &'a mut str {
fn from(value: StrMut<'a>) -> Self {
unsafe { core::str::from_utf8_unchecked_mut(value.inner.into()) }
}
}
impl<'a> From<StrMut<'a>> for &'a str {
fn from(value: StrMut<'a>) -> Self {
unsafe { core::str::from_utf8_unchecked(value.inner.into()) }
}
}