1
- use bstr:: { BStr , ByteSlice } ;
2
- use kstring:: { KString , KStringRef } ;
3
-
4
1
use crate :: { State , StateRef } ;
2
+ use bstr:: { BStr , BString , ByteSlice } ;
5
3
6
4
/// A container to encapsulate a tightly packed and typically unallocated byte value that isn't necessarily UTF8 encoded.
7
5
#[ derive( PartialEq , Eq , Debug , Hash , Ord , PartialOrd , Clone ) ]
8
6
#[ cfg_attr( feature = "serde" , derive( serde:: Serialize , serde:: Deserialize ) ) ]
9
- pub struct Value ( KString ) ;
7
+ // TODO: This should be some sort of 'smallbstring' - but can't use `kstring` here due to UTF8 requirement. 5% performance boost possible.
8
+ // What's really needed here is a representation that displays as string when serialized which helps with JSON.
9
+ // Maybe `smallvec` with display and serialization wrapper would do the trick?
10
+ pub struct Value ( BString ) ;
10
11
11
12
/// A reference container to encapsulate a tightly packed and typically unallocated byte value that isn't necessarily UTF8 encoded.
12
13
#[ derive( PartialEq , Eq , Debug , Hash , Ord , PartialOrd , Clone , Copy ) ]
13
14
#[ cfg_attr( feature = "serde" , derive( serde:: Serialize , serde:: Deserialize ) ) ]
14
- pub struct ValueRef < ' a > ( #[ cfg_attr( feature = "serde" , serde( borrow) ) ] KStringRef < ' a > ) ;
15
+ pub struct ValueRef < ' a > ( #[ cfg_attr( feature = "serde" , serde( borrow) ) ] & ' a [ u8 ] ) ;
15
16
16
17
/// Lifecycle
17
18
impl < ' a > ValueRef < ' a > {
18
19
/// Keep `input` as our value.
19
20
pub fn from_bytes ( input : & ' a [ u8 ] ) -> Self {
20
- Self ( KStringRef :: from_ref (
21
- // SAFETY: our API makes accessing that value as `str` impossible, so illformed UTF8 is never exposed as such.
22
- #[ allow( unsafe_code) ]
23
- unsafe {
24
- std:: str:: from_utf8_unchecked ( input)
25
- } ,
26
- ) )
21
+ Self ( input)
27
22
}
28
23
}
29
24
@@ -42,7 +37,7 @@ impl ValueRef<'_> {
42
37
43
38
impl < ' a > From < & ' a str > for ValueRef < ' a > {
44
39
fn from ( v : & ' a str ) -> Self {
45
- ValueRef ( v. into ( ) )
40
+ ValueRef ( v. as_bytes ( ) )
46
41
}
47
42
}
48
43
@@ -54,7 +49,7 @@ impl<'a> From<ValueRef<'a>> for Value {
54
49
55
50
impl From < & str > for Value {
56
51
fn from ( v : & str ) -> Self {
57
- Value ( KString :: from_ref ( v ) )
52
+ Value ( v . as_bytes ( ) . into ( ) )
58
53
}
59
54
}
60
55
0 commit comments