1
- use repr:: Maybe ;
1
+ use repr:: KjMaybe ;
2
2
use std:: mem:: MaybeUninit ;
3
3
4
4
/// # Safety
@@ -18,7 +18,7 @@ use std::mem::MaybeUninit;
18
18
/// `Own`s have a niche where the pointer to the owned data is null. This is
19
19
/// a valid instance of `Own`, but was decided by the `kj` authors to represent
20
20
/// `kj::none`. In Rust, it is guaranteed that an `Own` is nonnull, requiring
21
- /// `Maybe <Own<T>>` to represent a null `Own`.
21
+ /// `KjMaybe <Own<T>>` to represent a null `Own`.
22
22
///
23
23
/// Pointers are not optimized in this way, as `null` is a valid and meaningful
24
24
/// instance of a pointer.
@@ -53,42 +53,42 @@ unsafe impl<T> HasNiche for &mut T {
53
53
}
54
54
55
55
// In `kj`, `kj::Own<T>` are considered `none` in a `Maybe` if the data pointer is null
56
- unsafe impl < T > HasNiche for crate :: repr:: Own < T > {
57
- fn is_niche ( value : * const crate :: repr:: Own < T > ) -> bool {
56
+ unsafe impl < T > HasNiche for crate :: repr:: KjOwn < T > {
57
+ fn is_niche ( value : * const crate :: repr:: KjOwn < T > ) -> bool {
58
58
unsafe { ( * value) . as_ptr ( ) . is_null ( ) }
59
59
}
60
60
}
61
61
62
- /// Trait that is used as the bounds for what can be in a `kj_rs::Maybe `.
62
+ /// Trait that is used as the bounds for what can be in a `kj_rs::KjMaybe `.
63
63
///
64
64
/// # Safety
65
65
/// This trait should only be implemented from macro expansion and should
66
66
/// never be manually implemented. An unsound implementation of this trait
67
67
/// could result in undefined behavior when passed between languages.
68
68
///
69
- /// This trait contains all behavior we need to implement `Maybe <T: MaybeItem>`
69
+ /// This trait contains all behavior we need to implement `KjMaybe <T: MaybeItem>`
70
70
/// for every `T` we use, and additionally determines the type layout of
71
- /// the `Maybe <T>`. The only information we can know about `T` comes from
71
+ /// the `KjMaybe <T>`. The only information we can know about `T` comes from
72
72
/// this trait, so it must be capable of handling all behavior we want in
73
- /// `kj_rs::Maybe `.
73
+ /// `kj_rs::KjMaybe `.
74
74
///
75
75
/// Every function without a default depends on `MaybeItem::Discriminant`
76
76
/// and whether or not `T` implements [`HasNiche`]. Functions with defaults
77
77
/// use those functions to implement shared behavior, and simplfy the actual
78
- /// `Maybe <T>` implementation.
78
+ /// `KjMaybe <T>` implementation.
79
79
pub unsafe trait MaybeItem : Sized {
80
80
type Discriminant : Copy ;
81
- const NONE : Maybe < Self > ;
82
- fn some ( value : Self ) -> Maybe < Self > ;
83
- fn is_some ( value : & Maybe < Self > ) -> bool ;
84
- fn is_none ( value : & Maybe < Self > ) -> bool ;
85
- fn from_option ( value : Option < Self > ) -> Maybe < Self > {
81
+ const NONE : KjMaybe < Self > ;
82
+ fn some ( value : Self ) -> KjMaybe < Self > ;
83
+ fn is_some ( value : & KjMaybe < Self > ) -> bool ;
84
+ fn is_none ( value : & KjMaybe < Self > ) -> bool ;
85
+ fn from_option ( value : Option < Self > ) -> KjMaybe < Self > {
86
86
match value {
87
87
None => <Self as MaybeItem >:: NONE ,
88
88
Some ( val) => <Self as MaybeItem >:: some ( val) ,
89
89
}
90
90
}
91
- fn drop_in_place ( value : & mut Maybe < Self > ) {
91
+ fn drop_in_place ( value : & mut KjMaybe < Self > ) {
92
92
if <Self as MaybeItem >:: is_some ( value) {
93
93
unsafe {
94
94
value. some . assume_init_drop ( ) ;
@@ -104,23 +104,23 @@ macro_rules! impl_maybe_item_for_has_niche {
104
104
unsafe impl <T > MaybeItem for $ty {
105
105
type Discriminant = ( ) ;
106
106
107
- fn is_some( value: & Maybe <Self >) -> bool {
107
+ fn is_some( value: & KjMaybe <Self >) -> bool {
108
108
!<$ty as HasNiche >:: is_niche( value. some. as_ptr( ) )
109
109
}
110
110
111
- fn is_none( value: & Maybe <Self >) -> bool {
111
+ fn is_none( value: & KjMaybe <Self >) -> bool {
112
112
<$ty as HasNiche >:: is_niche( value. some. as_ptr( ) )
113
113
}
114
114
115
- const NONE : Maybe <Self > = {
116
- Maybe {
115
+ const NONE : KjMaybe <Self > = {
116
+ KjMaybe {
117
117
is_set: ( ) ,
118
118
some: MaybeUninit :: zeroed( ) ,
119
119
}
120
120
} ;
121
121
122
- fn some( value: Self ) -> Maybe <Self > {
123
- Maybe {
122
+ fn some( value: Self ) -> KjMaybe <Self > {
123
+ KjMaybe {
124
124
is_set: ( ) ,
125
125
some: MaybeUninit :: new( value)
126
126
}
@@ -140,23 +140,23 @@ macro_rules! impl_maybe_item_for_primitive {
140
140
unsafe impl MaybeItem for $ty {
141
141
type Discriminant = bool ;
142
142
143
- fn is_some( value: & Maybe <Self >) -> bool {
143
+ fn is_some( value: & KjMaybe <Self >) -> bool {
144
144
value. is_set
145
145
}
146
146
147
- fn is_none( value: & Maybe <Self >) -> bool {
147
+ fn is_none( value: & KjMaybe <Self >) -> bool {
148
148
!value. is_set
149
149
}
150
150
151
- const NONE : Maybe <Self > = {
152
- Maybe {
151
+ const NONE : KjMaybe <Self > = {
152
+ KjMaybe {
153
153
is_set: false ,
154
154
some: MaybeUninit :: uninit( ) ,
155
155
}
156
156
} ;
157
157
158
- fn some( value: Self ) -> Maybe <Self > {
159
- Maybe {
158
+ fn some( value: Self ) -> KjMaybe <Self > {
159
+ KjMaybe {
160
160
is_set: true ,
161
161
some: MaybeUninit :: new( value)
162
162
}
@@ -169,7 +169,7 @@ macro_rules! impl_maybe_item_for_primitive {
169
169
} ;
170
170
}
171
171
172
- impl_maybe_item_for_has_niche ! ( crate :: Own <T >, & T , & mut T ) ;
172
+ impl_maybe_item_for_has_niche ! ( crate :: KjOwn <T >, & T , & mut T ) ;
173
173
impl_maybe_item_for_primitive ! (
174
174
u8 , u16 , u32 , u64 , u128 , usize , i8 , i16 , i32 , i64 , i128 , isize , f32 , f64 , bool
175
175
) ;
@@ -180,29 +180,29 @@ pub(crate) mod repr {
180
180
use std:: fmt:: Debug ;
181
181
use std:: mem:: MaybeUninit ;
182
182
183
- /// A [`Maybe `] represents bindings to the `kj::Maybe` class.
183
+ /// A [`KjMaybe `] represents bindings to the `kj::Maybe` class.
184
184
/// It is an optional type, but represented using a struct, for alignment with kj.
185
185
///
186
186
/// # Layout
187
187
/// In kj, `Maybe` has 3 specializations, one without niche value optimization, and
188
188
/// two with it. In order to maintain an identical layout in Rust, we include an associated type
189
- /// in the [`MaybeItem`] trait, which determines the discriminant of the `Maybe <T: MaybeItem>`.
189
+ /// in the [`MaybeItem`] trait, which determines the discriminant of the `KjMaybe <T: MaybeItem>`.
190
190
///
191
191
/// ## Niche Value Optimization
192
192
/// This discriminant is used in tandem with the [`crate::maybe::HasNiche`] to implement
193
193
/// [`MaybeItem`] properly for values which have a niche, which use a discriminant of [`()`],
194
194
/// the unit type. All other types use [`bool`].
195
195
#[ repr( C ) ]
196
- pub struct Maybe < T : MaybeItem > {
196
+ pub struct KjMaybe < T : MaybeItem > {
197
197
pub ( super ) is_set : T :: Discriminant ,
198
198
pub ( super ) some : MaybeUninit < T > ,
199
199
}
200
200
201
- assert_eq_size ! ( Maybe <isize >, [ usize ; 2 ] ) ;
202
- assert_eq_size ! ( Maybe <& isize >, usize ) ;
203
- assert_eq_size ! ( Maybe <crate :: Own <isize >>, [ usize ; 2 ] ) ;
201
+ assert_eq_size ! ( KjMaybe <isize >, [ usize ; 2 ] ) ;
202
+ assert_eq_size ! ( KjMaybe <& isize >, usize ) ;
203
+ assert_eq_size ! ( KjMaybe <crate :: KjOwn <isize >>, [ usize ; 2 ] ) ;
204
204
205
- impl < T : MaybeItem > Maybe < T > {
205
+ impl < T : MaybeItem > KjMaybe < T > {
206
206
/// # Safety
207
207
/// This function shouldn't be used except by macro generation.
208
208
pub unsafe fn is_set ( & self ) -> T :: Discriminant {
@@ -215,8 +215,8 @@ pub(crate) mod repr {
215
215
pub const unsafe fn from_parts_unchecked (
216
216
is_set : T :: Discriminant ,
217
217
some : MaybeUninit < T > ,
218
- ) -> Maybe < T > {
219
- Maybe { is_set, some }
218
+ ) -> KjMaybe < T > {
219
+ KjMaybe { is_set, some }
220
220
}
221
221
222
222
pub fn is_some ( & self ) -> bool {
@@ -243,23 +243,23 @@ pub(crate) mod repr {
243
243
244
244
/// The [`Maybe::Some`] function serves the same purpose as an enum constructor.
245
245
///
246
- /// Constructing a `Maybe <T>::Some(val)` should only be possible with a valid
246
+ /// Constructing a `KjMaybe <T>::Some(val)` should only be possible with a valid
247
247
/// instance of `T` from Rust.
248
248
#[ allow( non_snake_case) ]
249
- pub fn Some ( value : T ) -> Maybe < T > {
249
+ pub fn Some ( value : T ) -> KjMaybe < T > {
250
250
T :: some ( value)
251
251
}
252
252
253
253
/// [`Maybe::None`] functions as a constructor for the none variant. It uses
254
254
/// a `const` instead of a function to match syntax with normal Rust enums.
255
255
///
256
- /// Constructing a `Maybe <T>::None` variant should always be possible from Rust.
256
+ /// Constructing a `KjMaybe <T>::None` variant should always be possible from Rust.
257
257
#[ allow( non_upper_case_globals, dead_code) ]
258
- pub const None : Maybe < T > = T :: NONE ;
258
+ pub const None : KjMaybe < T > = T :: NONE ;
259
259
}
260
260
261
- impl < T : MaybeItem > From < Maybe < T > > for Option < T > {
262
- fn from ( value : Maybe < T > ) -> Self {
261
+ impl < T : MaybeItem > From < KjMaybe < T > > for Option < T > {
262
+ fn from ( value : KjMaybe < T > ) -> Self {
263
263
if value. is_some ( ) {
264
264
// We can't move out of value so we copy it and forget it in
265
265
// order to perform a "manual" move out of value
@@ -272,13 +272,13 @@ pub(crate) mod repr {
272
272
}
273
273
}
274
274
275
- impl < T : MaybeItem > From < Option < T > > for Maybe < T > {
275
+ impl < T : MaybeItem > From < Option < T > > for KjMaybe < T > {
276
276
fn from ( value : Option < T > ) -> Self {
277
277
<T as MaybeItem >:: from_option ( value)
278
278
}
279
279
}
280
280
281
- impl < T : MaybeItem + Debug > Debug for Maybe < T > {
281
+ impl < T : MaybeItem + Debug > Debug for KjMaybe < T > {
282
282
fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
283
283
if self . is_none ( ) {
284
284
write ! ( f, "Maybe::None" )
@@ -290,13 +290,13 @@ pub(crate) mod repr {
290
290
}
291
291
}
292
292
293
- impl < T : MaybeItem > Default for Maybe < T > {
293
+ impl < T : MaybeItem > Default for KjMaybe < T > {
294
294
fn default ( ) -> Self {
295
295
T :: NONE
296
296
}
297
297
}
298
298
299
- impl < T : MaybeItem > Drop for Maybe < T > {
299
+ impl < T : MaybeItem > Drop for KjMaybe < T > {
300
300
fn drop ( & mut self ) {
301
301
T :: drop_in_place ( self ) ;
302
302
}
0 commit comments