-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbytestr.rs
More file actions
150 lines (130 loc) · 4.27 KB
/
bytestr.rs
File metadata and controls
150 lines (130 loc) · 4.27 KB
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
// TODO impl a more general one outside
use crate::*;
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
// currently no need for Default & new()
// TODO impl<B: AsRef<[u8]>, S: AsRef<str>> PartialEq&PartialOrd<S> for ByteStr<B>
// cannot due to similar problem with Value
pub struct ByteStr<B> {
// Invariant: bytes contains valid UTF-8
bytes: B,
}
// TODO(std) impl<'a> From<&'a str> for &'a [u8]
/*
impl<'a, B: From<&'a str> + ByteStorage> From<&'a str> for ByteStr<B> {
fn from(value: &'a str) -> Self {
// Invariant: value is a str so contains valid UTF-8.
ByteStr { bytes: value.into() }
}
}
*/
// remove when generic impl feasible
impl<'a> From<&'a str> for ByteStr<&'a [u8]> {
fn from(value: &'a str) -> Self {
// Invariant: value is a str so contains valid UTF-8.
ByteStr { bytes: value.as_bytes() }
}
}
#[cfg(feature = "alloc")]
impl From<String> for ByteStr<Vec<u8>> {
fn from(value: String) -> Self {
// Invariant: value is a String so contains valid UTF-8.
ByteStr { bytes: value.into_bytes() }
}
}
// remove when generic impl feasible
#[cfg(feature = "bytes")]
impl ByteStr<Bytes> {
// impl From<&'static str> for ByteStr<Bytes> if not conflict with below
pub const fn from_static(value: &'static str) -> Self {
ByteStr {
// Invariant: value is a str so contains valid UTF-8.
// bytes: Bytes::from(value), // no const trait fn
bytes: Bytes::from_static(value.as_bytes()),
}
}
}
// remove when generic impl feasible
#[cfg(feature = "bytes")]
impl<'a> From<&'a str> for ByteStr<Bytes> {
fn from(value: &'a str) -> Self {
ByteStr {
// Invariant: value is a str so contains valid UTF-8.
// bytes: Bytes::from(value), // not impled? what about below tests?
bytes: Bytes::copy_from_slice(value.as_bytes()),
}
}
}
#[cfg(all(feature = "alloc", feature = "bytes"))]
impl From<String> for ByteStr<Bytes> {
fn from(value: String) -> Self {
// Invariant: value is a String so contains valid UTF-8.
ByteStr { bytes: value.into() }
}
}
impl<B: AsRef<[u8]>> ByteStr<B> {
pub fn as_bytes(&self) -> &[u8] {
self.bytes.as_ref()
}
}
impl<B: AsRef<[u8]> + ByteStorage> ByteStr<B> {
pub fn as_str(&self) -> &str {
let b: &[u8] = self.bytes.as_ref();
// Safety: the invariant of `bytes` is that it contains valid UTF-8.
unsafe { core::str::from_utf8_unchecked(b) }
}
// DO NOT impl TryFrom<B> keeping consistency to std
// https://internals.rust-lang.org/t/20078/
// TODO Result<Self, (B, core::str::Utf8Error)> ?
pub fn from_utf8(bytes: B) -> Result<Self, core::str::Utf8Error> {
let _ = core::str::from_utf8(bytes.as_ref())?;
Ok(ByteStr { bytes })
}
// cannot impl<B> From<ByteStr<B>> for B
pub fn leak_bytes(self) -> B {
self.bytes
}
}
impl<B: AsRef<[u8]>> AsRef<[u8]> for ByteStr<B> {
fn as_ref(&self) -> &[u8] {
self.as_bytes()
}
}
impl<B: AsRef<[u8]> + ByteStorage> AsRef<str> for ByteStr<B> {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl<B> ByteStr<B> {
pub fn map_bytes<B2>(self, f: fn(B) -> B2) -> ByteStr<B2> {
ByteStr { bytes: f(self.bytes) }
}
}
impl<B: AsRef<[u8]> + ByteStorage> core::fmt::Debug for ByteStr<B> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
core::fmt::Debug::fmt(self.as_str(), f)
}
}
impl<B: AsRef<[u8]> + ByteStorage> core::fmt::Display for ByteStr<B> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
core::fmt::Display::fmt(self.as_str(), f)
}
}
#[test]
#[allow(path_statements)]
const fn test() {
trait AssertImpl { const ASSERT: () = (); }
struct A<T>(T);
impl<'a, T: From<&'a str>> AssertImpl for A<T> { const ASSERT: () = (); }
// A::<&[u8]>::ASSERT;
A::<Vec<u8>>::ASSERT;
// A::<Box<[u8]>>::ASSERT;
A::<Bytes>::ASSERT;
A::<ByteStr<&[u8]>>::ASSERT;
A::<ByteStr<Bytes>>::ASSERT;
struct B<T>(T);
impl<T: From<&'static str>> AssertImpl for B<T> { const ASSERT: () = (); }
// B::<&'static [u8]>::ASSERT;
B::<Bytes>::ASSERT;
B::<ByteStr<&'static [u8]>>::ASSERT;
B::<ByteStr<Bytes>>::ASSERT;
}