Skip to content

Commit 8646617

Browse files
committed
Introduce the ExplicitLifetype type ref hint
1 parent 87465c6 commit 8646617

File tree

4 files changed

+43
-18
lines changed

4 files changed

+43
-18
lines changed

binding-generator/src/type_ref/types.rs

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ pub enum TypeRefTypeHint {
3434
/// Make sure to pass TraitClass as a concrete type, not as a trait object, it's used for property setters as we don't want
3535
/// to be able to use `BoxedRef` there.
3636
TraitClassConcrete,
37+
/// References in Rust will get an indicated lifetime instead of the implied one
38+
ExplicitLifetime(Lifetime),
3739
}
3840

3941
impl TypeRefTypeHint {
@@ -51,41 +53,60 @@ impl TypeRefTypeHint {
5153
match self {
5254
Self::Nullable => Self::None,
5355
Self::NullableSlice => Self::Slice,
54-
recursable => recursable,
56+
Self::None
57+
| Self::Slice
58+
| Self::LenForSlice(_, _)
59+
| Self::StringAsBytes(_)
60+
| Self::CharAsRustChar
61+
| Self::CharPtrSingleChar
62+
| Self::PrimitivePtrAsRaw
63+
| Self::AddArrayLength(_)
64+
| Self::BoxedAsRef(_, _, _)
65+
| Self::TraitClassConcrete
66+
| Self::ExplicitLifetime(_) => self.clone(),
5567
}
5668
}
5769

5870
pub fn nullability(&self) -> Nullability {
5971
match self {
60-
TypeRefTypeHint::Nullable | TypeRefTypeHint::NullableSlice => Nullability::Nullable,
61-
TypeRefTypeHint::None
62-
| TypeRefTypeHint::Slice
63-
| TypeRefTypeHint::LenForSlice(_, _)
64-
| TypeRefTypeHint::StringAsBytes(_)
65-
| TypeRefTypeHint::CharAsRustChar
66-
| TypeRefTypeHint::CharPtrSingleChar
67-
| TypeRefTypeHint::PrimitivePtrAsRaw
68-
| TypeRefTypeHint::AddArrayLength(_)
69-
| TypeRefTypeHint::BoxedAsRef(_, _, _)
70-
| TypeRefTypeHint::TraitClassConcrete => Nullability::NotNullable,
72+
Self::Nullable | Self::NullableSlice => Nullability::Nullable,
73+
Self::None
74+
| Self::Slice
75+
| Self::LenForSlice(_, _)
76+
| Self::StringAsBytes(_)
77+
| Self::CharAsRustChar
78+
| Self::CharPtrSingleChar
79+
| Self::PrimitivePtrAsRaw
80+
| Self::AddArrayLength(_)
81+
| Self::BoxedAsRef(_, _, _)
82+
| Self::TraitClassConcrete
83+
| Self::ExplicitLifetime(_) => Nullability::NotNullable,
7184
}
7285
}
7386

7487
pub fn as_slice_len(&self) -> Option<(&[String], usize)> {
75-
if let TypeRefTypeHint::LenForSlice(ptr_arg, len_div) = self {
88+
if let Self::LenForSlice(ptr_arg, len_div) = self {
7689
Some((ptr_arg, *len_div))
7790
} else {
7891
None
7992
}
8093
}
8194

8295
pub fn as_boxed_as_ref(&self) -> Option<(Constness, &'static str, Lifetime)> {
83-
if let TypeRefTypeHint::BoxedAsRef(constness, cpp_name, lifetime) = self {
96+
if let Self::BoxedAsRef(constness, cpp_name, lifetime) = self {
8497
Some((*constness, cpp_name, *lifetime))
8598
} else {
8699
None
87100
}
88101
}
102+
103+
pub fn as_explicit_lifetime(&self) -> Option<Lifetime> {
104+
if let Self::ExplicitLifetime(lifetime) = self {
105+
Some(*lifetime)
106+
} else {
107+
None
108+
}
109+
}
89110
}
90111

91112
#[derive(Clone, Copy, Debug)]

binding-generator/src/writer/rust_native/func.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,8 @@ impl RustNativeGeneratedElement for Func<'_, '_> {
237237
};
238238
for (name, arg) in &args {
239239
let arg_type_ref = arg.type_ref();
240-
let arg_as_slice_len = arg_type_ref.type_hint().as_slice_len();
240+
let arg_type_hint = arg_type_ref.type_hint();
241+
let arg_as_slice_len = arg_type_hint.as_slice_len();
241242
let arg_kind = arg_type_ref.kind();
242243
let render_lane = arg_type_ref.render_lane();
243244
let render_lane = render_lane.to_dyn();
@@ -257,7 +258,9 @@ impl RustNativeGeneratedElement for Func<'_, '_> {
257258
if !arg_as_slice_len.is_some() {
258259
let lt = boxed_ref_arg
259260
.filter(|(_, boxed_arg_name, _)| *boxed_arg_name == name)
260-
.map_or(Lifetime::Elided, |(_, _, lt)| lt);
261+
.map(|(_, _, lt)| lt)
262+
.or_else(|| arg_type_hint.as_explicit_lifetime())
263+
.unwrap_or(Lifetime::Elided);
261264
decl_args.push(render_lane.rust_arg_func_decl(name, lt).into());
262265
}
263266
pre_post_arg_handle(render_lane.rust_arg_pre_call(name, &function_props), &mut pre_call_args);

binding-generator/src/writer/rust_native/renderer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,6 @@ impl TypeRefRenderer<'_> for RustReturnRenderer {
281281
}
282282

283283
fn recurse(&self) -> Self::Recursed {
284-
RustRenderer::new(NameStyle::Reference(self.turbo_fish_style), Lifetime::Elided)
284+
RustRenderer::new(NameStyle::Reference(self.turbo_fish_style), self.lifetime)
285285
}
286286
}

binding-generator/src/writer/rust_native/type_ref.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,8 @@ impl TypeRefExt for TypeRef<'_, '_> {
218218
}
219219

220220
fn rust_name(&self, name_style: NameStyle) -> Cow<str> {
221-
self.rust_name_ext(name_style, Lifetime::Elided)
221+
let lt = self.type_hint().as_explicit_lifetime().unwrap_or(Lifetime::Elided);
222+
self.rust_name_ext(name_style, lt)
222223
}
223224

224225
fn rust_name_ext(&self, name_style: NameStyle, lifetime: Lifetime) -> Cow<str> {

0 commit comments

Comments
 (0)