@@ -83,17 +83,35 @@ pub unsafe fn consume_region(ptr: *mut Region) -> Vec<u8> {
83
83
/// And since `size` is one of the parameters, it is important to pass in the exact same capacity.
84
84
///
85
85
/// See: <https://doc.rust-lang.org/stable/alloc/alloc/trait.GlobalAlloc.html#safety-2>
86
- pub unsafe trait RegionSource : AsRef < [ u8 ] > {
86
+ pub unsafe trait RegionSource {
87
+ fn ptr ( & self ) -> * const u8 ;
88
+ fn len ( & self ) -> usize ;
87
89
fn capacity ( & self ) -> usize ;
88
90
}
89
91
90
92
unsafe impl RegionSource for & [ u8 ] {
93
+ fn ptr ( & self ) -> * const u8 {
94
+ self . as_ptr ( )
95
+ }
96
+
97
+ fn len ( & self ) -> usize {
98
+ ( * self ) . len ( )
99
+ }
100
+
91
101
fn capacity ( & self ) -> usize {
92
102
self . len ( )
93
103
}
94
104
}
95
105
96
106
unsafe impl RegionSource for Vec < u8 > {
107
+ fn ptr ( & self ) -> * const u8 {
108
+ self . as_ptr ( )
109
+ }
110
+
111
+ fn len ( & self ) -> usize {
112
+ self . len ( )
113
+ }
114
+
97
115
fn capacity ( & self ) -> usize {
98
116
self . capacity ( )
99
117
}
@@ -103,6 +121,14 @@ unsafe impl<T: ?Sized> RegionSource for &T
103
121
where
104
122
T : RegionSource ,
105
123
{
124
+ fn ptr ( & self ) -> * const u8 {
125
+ ( * * self ) . ptr ( )
126
+ }
127
+
128
+ fn len ( & self ) -> usize {
129
+ ( * * self ) . len ( )
130
+ }
131
+
106
132
fn capacity ( & self ) -> usize {
107
133
( * * self ) . capacity ( )
108
134
}
@@ -116,12 +142,12 @@ pub fn build_region<S>(data: S) -> Box<Region>
116
142
where
117
143
S : RegionSource ,
118
144
{
119
- let data_slice = data . as_ref ( ) ;
120
- let data_ptr = data_slice . as_ptr ( ) as usize ;
145
+ // Well, this technically violates pointer provenance rules.
146
+ // But there isn't a stable API for it, so that's the best we can do, I guess.
121
147
build_region_from_components (
122
- u32:: try_from ( data_ptr ) . expect ( "pointer doesn't fit in u32" ) ,
148
+ u32:: try_from ( data . ptr ( ) as usize ) . expect ( "pointer doesn't fit in u32" ) ,
123
149
u32:: try_from ( data. capacity ( ) ) . expect ( "capacity doesn't fit in u32" ) ,
124
- u32:: try_from ( data_slice . len ( ) ) . expect ( "length doesn't fit in u32" ) ,
150
+ u32:: try_from ( data . len ( ) ) . expect ( "length doesn't fit in u32" ) ,
125
151
)
126
152
}
127
153
0 commit comments