1
- use libc:: size_t;
2
- use std:: os:: raw:: { c_char , c_ushort} ;
1
+ use libc:: { c_char , size_t} ;
2
+ use std:: { marker :: PhantomData , os:: raw:: { c_ushort} } ;
3
3
4
- /// A read-only view on a Rust utf-8 string or byte array.
4
+ /// A read-only view on a Rust byte slice.
5
+ ///
5
6
/// This is used to pass data from crustls to callback functions provided
6
- /// by the user of the API. The `data` is not NUL-terminated.
7
+ /// by the user of the API.
7
8
/// `len` indicates the number of bytes than can be safely read.
8
- /// A `len` of 0 is used to represent a missing value.
9
+ /// A `len` of 0 is used to represent a missing value OR an empty slice .
9
10
///
10
- /// The memmory exposed is available for the duration of the call (e.g.
11
+ /// The memory exposed is available for the duration of the call (e.g.
11
12
/// when passed to a callback) and must be copied if the values are
12
13
/// needed for longer.
13
- ///
14
14
#[ allow( non_camel_case_types) ]
15
15
#[ repr( C ) ]
16
- pub struct rustls_string {
17
- data : * const c_char ,
16
+ pub struct rustls_slice_bytes < ' a > {
17
+ data : * const u8 ,
18
18
len : size_t ,
19
+ phantom : PhantomData < & ' a [ u8 ] > ,
19
20
}
20
21
21
- impl < ' a > From < & ' a String > for rustls_string {
22
- fn from ( s : & String ) -> Self {
23
- rustls_string {
24
- data : s. as_ptr ( ) as * const c_char ,
22
+ impl < ' a > From < & ' a [ u8 ] > for rustls_slice_bytes < ' a > {
23
+ fn from ( s : & [ u8 ] ) -> Self {
24
+ rustls_slice_bytes {
25
+ data : s. as_ptr ( ) as * const u8 ,
25
26
len : s. len ( ) as size_t ,
27
+ phantom : PhantomData ,
26
28
}
27
29
}
28
30
}
29
31
30
- impl < ' a > From < & ' a str > for rustls_string {
31
- fn from ( s : & str ) -> Self {
32
- rustls_string {
33
- data : s. as_ptr ( ) as * const c_char ,
34
- len : s. len ( ) as size_t ,
35
- }
36
- }
32
+ /// A read-only view on a vector of Rust byte slices.
33
+ ///
34
+ /// This is used to pass data from crustls to callback functions provided
35
+ /// by the user of the API. The `data` is an array of `rustls_slice_bytes`
36
+ /// structures with `len` elements.
37
+ ///
38
+ /// The memory exposed is available for the duration of the call (e.g.
39
+ /// when passed to a callback) and must be copied if the values are
40
+ /// needed for longer.
41
+ #[ allow( non_camel_case_types) ]
42
+ #[ repr( C ) ]
43
+ pub struct rustls_vec_slice_bytes < ' a > {
44
+ data : * const rustls_slice_bytes < ' a > ,
45
+ len : size_t ,
37
46
}
38
47
39
- impl < ' a > From < & ' a [ u8 ] > for rustls_string {
40
- fn from ( s : & [ u8 ] ) -> Self {
41
- rustls_string {
42
- data : s. as_ptr ( ) as * const c_char ,
43
- len : s. len ( ) as size_t ,
48
+ impl < ' a > From < & ' a [ & [ u8 ] ] > for rustls_vec_slice_bytes < ' a > {
49
+ fn from ( input : & ' a [ & [ u8 ] ] ) -> Self {
50
+ let mut output: Vec < rustls_slice_bytes > = vec ! [ ] ;
51
+ for b in input {
52
+ let b: & [ u8 ] = b;
53
+ output. push ( b. into ( ) ) ;
44
54
}
45
- }
46
- }
47
-
48
- pub ( crate ) fn rustls_strings < ' a > ( values : Option < & ' a [ & ' a [ u8 ] ] > ) -> Vec < rustls_string > {
49
- let mut strings: Vec < rustls_string > = Vec :: new ( ) ;
50
- match values {
51
- Some ( values) => {
52
- for entry in values. iter ( ) {
53
- strings. push ( rustls_string:: from ( * entry) )
54
- }
55
+ rustls_vec_slice_bytes {
56
+ data : output. as_ptr ( ) ,
57
+ len : output. len ( ) ,
55
58
}
56
- None => ( ) ,
57
- } ;
58
- strings
59
+ }
59
60
}
60
61
61
- /// A read-only view on a list of Rust utf-8 strings or byte arrays .
62
+ /// A read-only view on a Rust utf-8 string slice .
62
63
/// This is used to pass data from crustls to callback functions provided
63
- /// by the user of the API. The `data` is an array of `rustls_string`
64
- /// structures with `len` elements.
64
+ /// by the user of the API. The `data` is not NUL-terminated.
65
+ /// `len` indicates the number of bytes than can be safely read.
66
+ /// A `len` of 0 is used to represent a missing value OR an empty string.
65
67
///
66
- /// The memmory exposed is available for the duration of the call (e.g.
68
+ /// The memory exposed is available for the duration of the call (e.g.
67
69
/// when passed to a callback) and must be copied if the values are
68
70
/// needed for longer.
69
- ///
70
71
#[ allow( non_camel_case_types) ]
71
72
#[ repr( C ) ]
72
- pub struct rustls_vec_string {
73
- data : * const rustls_string ,
73
+ pub struct rustls_str < ' a > {
74
+ data : * const c_char ,
74
75
len : size_t ,
76
+ phantom : PhantomData < & ' a str > ,
75
77
}
76
78
77
- impl < ' a > From < & ' a Vec < rustls_string > > for rustls_vec_string {
78
- fn from ( values : & Vec < rustls_string > ) -> Self {
79
- rustls_vec_string {
80
- data : values. as_ptr ( ) ,
81
- len : values. len ( ) ,
79
+ impl < ' a > From < & ' a str > for rustls_str < ' a > {
80
+ fn from ( s : & str ) -> Self {
81
+ rustls_str {
82
+ data : s. as_ptr ( ) as * const c_char ,
83
+ len : s. len ( ) as size_t ,
84
+ phantom : PhantomData ,
85
+ }
86
+ }
87
+ }
88
+
89
+ #[ allow( non_camel_case_types) ]
90
+ #[ repr( C ) ]
91
+ pub struct rustls_vec_str < ' a > {
92
+ data : * const rustls_str < ' a > ,
93
+ len : size_t ,
94
+ }
95
+
96
+ impl < ' a > From < & ' a [ String ] > for rustls_vec_str < ' a > {
97
+ fn from ( input : & ' a [ String ] ) -> Self {
98
+ let mut output: Vec < rustls_str > = vec ! [ ] ;
99
+ for b in input {
100
+ let b: & str = b;
101
+ output. push ( b. into ( ) ) ;
102
+ }
103
+ rustls_vec_str {
104
+ data : output. as_ptr ( ) ,
105
+ len : output. len ( ) ,
82
106
}
83
107
}
84
108
}
@@ -87,10 +111,9 @@ impl<'a> From<&'a Vec<rustls_string>> for rustls_vec_string {
87
111
/// This is used to pass data from crustls to callback functions provided
88
112
/// by the user of the API. The `data` is an array of `len` 16 bit values.
89
113
///
90
- /// The memmory exposed is available for the duration of the call (e.g.
114
+ /// The memory exposed is available for the duration of the call (e.g.
91
115
/// when passed to a callback) and must be copied if the values are
92
116
/// needed for longer.
93
- ///
94
117
#[ allow( non_camel_case_types) ]
95
118
#[ repr( C ) ]
96
119
pub struct rustls_vec_ushort {
0 commit comments