Skip to content

Commit 4c3f0ea

Browse files
authored
[workerd-cxx] unifying KjMaybe and KjOwn names (#63)
I want it to be very explicit that these are kj types.
1 parent d116bd1 commit 4c3f0ea

File tree

18 files changed

+213
-207
lines changed

18 files changed

+213
-207
lines changed

gen/src/write.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,11 @@ fn pick_includes_and_builtins(out: &mut OutFile, apis: &[Api]) {
233233
Type::SliceRef(_) => out.builtin.rust_slice = true,
234234
Type::Array(_) => out.include.array = true,
235235
Type::Ref(_) | Type::Void(_) | Type::Ptr(_) => {}
236-
Type::Future(_) | Type::Maybe(_) | Type::Own(_) | Type::KjRc(_) | Type::KjArc(_) => {
236+
Type::Future(_)
237+
| Type::KjMaybe(_)
238+
| Type::KjOwn(_)
239+
| Type::KjRc(_)
240+
| Type::KjArc(_) => {
237241
out.include.kj_rs = true;
238242
}
239243
}
@@ -1039,7 +1043,7 @@ fn write_rust_function_shim_impl(
10391043
writeln!(out, " {{");
10401044
sig.args
10411045
.iter()
1042-
.filter(|arg| matches!(arg.ty, Type::Own(_)))
1046+
.filter(|arg| matches!(arg.ty, Type::KjOwn(_)))
10431047
.for_each(|arg_own| {
10441048
writeln!(
10451049
out,
@@ -1243,7 +1247,7 @@ fn write_type(out: &mut OutFile, ty: &Type) {
12431247
write_type(out, &ptr.inner);
12441248
write!(out, ">");
12451249
}
1246-
Type::Own(ptr) => {
1250+
Type::KjOwn(ptr) => {
12471251
write!(out, "::kj::Own<");
12481252
write_type(out, &ptr.inner);
12491253
write!(out, ">");
@@ -1268,7 +1272,7 @@ fn write_type(out: &mut OutFile, ty: &Type) {
12681272
write_type(out, &ptr.inner);
12691273
write!(out, ">");
12701274
}
1271-
Type::Maybe(ptr) => {
1275+
Type::KjMaybe(ptr) => {
12721276
write!(out, "::kj::Maybe<");
12731277
write_type(out, &ptr.inner);
12741278
write!(out, ">");
@@ -1363,13 +1367,13 @@ fn write_space_after_type(out: &mut OutFile, ty: &Type) {
13631367
Type::Ident(_)
13641368
| Type::RustBox(_)
13651369
| Type::UniquePtr(_)
1366-
| Type::Own(_)
1370+
| Type::KjOwn(_)
13671371
| Type::KjRc(_)
13681372
| Type::KjArc(_)
13691373
| Type::SharedPtr(_)
13701374
| Type::WeakPtr(_)
13711375
| Type::Str(_)
1372-
| Type::Maybe(_)
1376+
| Type::KjMaybe(_)
13731377
| Type::CxxVector(_)
13741378
| Type::RustVec(_)
13751379
| Type::SliceRef(_)

kj-rs/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use awaiter::WakerRef;
44
pub use crate::ffi::KjWaker;
55
pub use awaiter::PromiseAwaiter;
66
pub use future::FuturePollStatus;
7-
pub use maybe::repr::Maybe;
8-
pub use own::repr::Own;
7+
pub use maybe::repr::KjMaybe;
8+
pub use own::repr::KjOwn;
99
pub use promise::KjPromise;
1010
pub use promise::KjPromiseNodeImpl;
1111
pub use promise::OwnPromiseNode;

kj-rs/maybe.rs

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use repr::Maybe;
1+
use repr::KjMaybe;
22
use std::mem::MaybeUninit;
33

44
/// # Safety
@@ -18,7 +18,7 @@ use std::mem::MaybeUninit;
1818
/// `Own`s have a niche where the pointer to the owned data is null. This is
1919
/// a valid instance of `Own`, but was decided by the `kj` authors to represent
2020
/// `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`.
2222
///
2323
/// Pointers are not optimized in this way, as `null` is a valid and meaningful
2424
/// instance of a pointer.
@@ -53,42 +53,42 @@ unsafe impl<T> HasNiche for &mut T {
5353
}
5454

5555
// 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 {
5858
unsafe { (*value).as_ptr().is_null() }
5959
}
6060
}
6161

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`.
6363
///
6464
/// # Safety
6565
/// This trait should only be implemented from macro expansion and should
6666
/// never be manually implemented. An unsound implementation of this trait
6767
/// could result in undefined behavior when passed between languages.
6868
///
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>`
7070
/// 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
7272
/// this trait, so it must be capable of handling all behavior we want in
73-
/// `kj_rs::Maybe`.
73+
/// `kj_rs::KjMaybe`.
7474
///
7575
/// Every function without a default depends on `MaybeItem::Discriminant`
7676
/// and whether or not `T` implements [`HasNiche`]. Functions with defaults
7777
/// use those functions to implement shared behavior, and simplfy the actual
78-
/// `Maybe<T>` implementation.
78+
/// `KjMaybe<T>` implementation.
7979
pub unsafe trait MaybeItem: Sized {
8080
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> {
8686
match value {
8787
None => <Self as MaybeItem>::NONE,
8888
Some(val) => <Self as MaybeItem>::some(val),
8989
}
9090
}
91-
fn drop_in_place(value: &mut Maybe<Self>) {
91+
fn drop_in_place(value: &mut KjMaybe<Self>) {
9292
if <Self as MaybeItem>::is_some(value) {
9393
unsafe {
9494
value.some.assume_init_drop();
@@ -104,23 +104,23 @@ macro_rules! impl_maybe_item_for_has_niche {
104104
unsafe impl<T> MaybeItem for $ty {
105105
type Discriminant = ();
106106

107-
fn is_some(value: &Maybe<Self>) -> bool {
107+
fn is_some(value: &KjMaybe<Self>) -> bool {
108108
!<$ty as HasNiche>::is_niche(value.some.as_ptr())
109109
}
110110

111-
fn is_none(value: &Maybe<Self>) -> bool {
111+
fn is_none(value: &KjMaybe<Self>) -> bool {
112112
<$ty as HasNiche>::is_niche(value.some.as_ptr())
113113
}
114114

115-
const NONE: Maybe<Self> = {
116-
Maybe {
115+
const NONE: KjMaybe<Self> = {
116+
KjMaybe {
117117
is_set: (),
118118
some: MaybeUninit::zeroed(),
119119
}
120120
};
121121

122-
fn some(value: Self) -> Maybe<Self> {
123-
Maybe {
122+
fn some(value: Self) -> KjMaybe<Self> {
123+
KjMaybe {
124124
is_set: (),
125125
some: MaybeUninit::new(value)
126126
}
@@ -140,23 +140,23 @@ macro_rules! impl_maybe_item_for_primitive {
140140
unsafe impl MaybeItem for $ty {
141141
type Discriminant = bool;
142142

143-
fn is_some(value: &Maybe<Self>) -> bool {
143+
fn is_some(value: &KjMaybe<Self>) -> bool {
144144
value.is_set
145145
}
146146

147-
fn is_none(value: &Maybe<Self>) -> bool {
147+
fn is_none(value: &KjMaybe<Self>) -> bool {
148148
!value.is_set
149149
}
150150

151-
const NONE: Maybe<Self> = {
152-
Maybe {
151+
const NONE: KjMaybe<Self> = {
152+
KjMaybe {
153153
is_set: false,
154154
some: MaybeUninit::uninit(),
155155
}
156156
};
157157

158-
fn some(value: Self) -> Maybe<Self> {
159-
Maybe {
158+
fn some(value: Self) -> KjMaybe<Self> {
159+
KjMaybe {
160160
is_set: true,
161161
some: MaybeUninit::new(value)
162162
}
@@ -169,7 +169,7 @@ macro_rules! impl_maybe_item_for_primitive {
169169
};
170170
}
171171

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);
173173
impl_maybe_item_for_primitive!(
174174
u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize, f32, f64, bool
175175
);
@@ -180,29 +180,29 @@ pub(crate) mod repr {
180180
use std::fmt::Debug;
181181
use std::mem::MaybeUninit;
182182

183-
/// A [`Maybe`] represents bindings to the `kj::Maybe` class.
183+
/// A [`KjMaybe`] represents bindings to the `kj::Maybe` class.
184184
/// It is an optional type, but represented using a struct, for alignment with kj.
185185
///
186186
/// # Layout
187187
/// In kj, `Maybe` has 3 specializations, one without niche value optimization, and
188188
/// 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>`.
190190
///
191191
/// ## Niche Value Optimization
192192
/// This discriminant is used in tandem with the [`crate::maybe::HasNiche`] to implement
193193
/// [`MaybeItem`] properly for values which have a niche, which use a discriminant of [`()`],
194194
/// the unit type. All other types use [`bool`].
195195
#[repr(C)]
196-
pub struct Maybe<T: MaybeItem> {
196+
pub struct KjMaybe<T: MaybeItem> {
197197
pub(super) is_set: T::Discriminant,
198198
pub(super) some: MaybeUninit<T>,
199199
}
200200

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]);
204204

205-
impl<T: MaybeItem> Maybe<T> {
205+
impl<T: MaybeItem> KjMaybe<T> {
206206
/// # Safety
207207
/// This function shouldn't be used except by macro generation.
208208
pub unsafe fn is_set(&self) -> T::Discriminant {
@@ -215,8 +215,8 @@ pub(crate) mod repr {
215215
pub const unsafe fn from_parts_unchecked(
216216
is_set: T::Discriminant,
217217
some: MaybeUninit<T>,
218-
) -> Maybe<T> {
219-
Maybe { is_set, some }
218+
) -> KjMaybe<T> {
219+
KjMaybe { is_set, some }
220220
}
221221

222222
pub fn is_some(&self) -> bool {
@@ -243,23 +243,23 @@ pub(crate) mod repr {
243243

244244
/// The [`Maybe::Some`] function serves the same purpose as an enum constructor.
245245
///
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
247247
/// instance of `T` from Rust.
248248
#[allow(non_snake_case)]
249-
pub fn Some(value: T) -> Maybe<T> {
249+
pub fn Some(value: T) -> KjMaybe<T> {
250250
T::some(value)
251251
}
252252

253253
/// [`Maybe::None`] functions as a constructor for the none variant. It uses
254254
/// a `const` instead of a function to match syntax with normal Rust enums.
255255
///
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.
257257
#[allow(non_upper_case_globals, dead_code)]
258-
pub const None: Maybe<T> = T::NONE;
258+
pub const None: KjMaybe<T> = T::NONE;
259259
}
260260

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 {
263263
if value.is_some() {
264264
// We can't move out of value so we copy it and forget it in
265265
// order to perform a "manual" move out of value
@@ -272,13 +272,13 @@ pub(crate) mod repr {
272272
}
273273
}
274274

275-
impl<T: MaybeItem> From<Option<T>> for Maybe<T> {
275+
impl<T: MaybeItem> From<Option<T>> for KjMaybe<T> {
276276
fn from(value: Option<T>) -> Self {
277277
<T as MaybeItem>::from_option(value)
278278
}
279279
}
280280

281-
impl<T: MaybeItem + Debug> Debug for Maybe<T> {
281+
impl<T: MaybeItem + Debug> Debug for KjMaybe<T> {
282282
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
283283
if self.is_none() {
284284
write!(f, "Maybe::None")
@@ -290,13 +290,13 @@ pub(crate) mod repr {
290290
}
291291
}
292292

293-
impl<T: MaybeItem> Default for Maybe<T> {
293+
impl<T: MaybeItem> Default for KjMaybe<T> {
294294
fn default() -> Self {
295295
T::NONE
296296
}
297297
}
298298

299-
impl<T: MaybeItem> Drop for Maybe<T> {
299+
impl<T: MaybeItem> Drop for KjMaybe<T> {
300300
fn drop(&mut self) {
301301
T::drop_in_place(self);
302302
}

0 commit comments

Comments
 (0)