Skip to content

Commit

Permalink
when a struct has no pointer fields, we can elide lifetime in impl bl…
Browse files Browse the repository at this point in the history
…ocks
  • Loading branch information
dwrensha committed Nov 15, 2024
1 parent 777de48 commit 55ff380
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 16 deletions.
8 changes: 4 additions & 4 deletions capnp-rpc/src/rpc_capnp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3060,7 +3060,7 @@ pub mod finish {
}
}

impl<'a> Reader<'a> {
impl Reader<'_> {
pub fn reborrow(&self) -> Reader<'_> {
Self { ..*self }
}
Expand Down Expand Up @@ -3815,7 +3815,7 @@ pub mod release {
}
}

impl<'a> Reader<'a> {
impl Reader<'_> {
pub fn reborrow(&self) -> Reader<'_> {
Self { ..*self }
}
Expand Down Expand Up @@ -4450,7 +4450,7 @@ pub mod disembargo {
}
}

impl<'a> Reader<'a> {
impl Reader<'_> {
pub fn reborrow(&self) -> Reader<'_> {
Self { ..*self }
}
Expand Down Expand Up @@ -7511,7 +7511,7 @@ pub mod promised_answer {
}
}

impl<'a> Reader<'a> {
impl Reader<'_> {
pub fn reborrow(&self) -> Reader<'_> {
Self { ..*self }
}
Expand Down
10 changes: 5 additions & 5 deletions capnp-rpc/src/rpc_twoparty_capnp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ pub mod vat_id {
}
}

impl<'a> Reader<'a> {
impl Reader<'_> {
pub fn reborrow(&self) -> Reader<'_> {
Self { ..*self }
}
Expand Down Expand Up @@ -457,7 +457,7 @@ pub mod provision_id {
}
}

impl<'a> Reader<'a> {
impl Reader<'_> {
pub fn reborrow(&self) -> Reader<'_> {
Self { ..*self }
}
Expand Down Expand Up @@ -731,7 +731,7 @@ pub mod recipient_id {
}
}

impl<'a> Reader<'a> {
impl Reader<'_> {
pub fn reborrow(&self) -> Reader<'_> {
Self { ..*self }
}
Expand Down Expand Up @@ -974,7 +974,7 @@ pub mod third_party_cap_id {
}
}

impl<'a> Reader<'a> {
impl Reader<'_> {
pub fn reborrow(&self) -> Reader<'_> {
Self { ..*self }
}
Expand Down Expand Up @@ -1218,7 +1218,7 @@ pub mod join_key_part {
}
}

impl<'a> Reader<'a> {
impl Reader<'_> {
pub fn reborrow(&self) -> Reader<'_> {
Self { ..*self }
}
Expand Down
12 changes: 6 additions & 6 deletions capnp/src/schema_capnp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5393,7 +5393,7 @@ pub mod field {
}
}

impl<'a> Reader<'a> {
impl Reader<'_> {
pub fn reborrow(&self) -> Reader<'_> {
Self { ..*self }
}
Expand Down Expand Up @@ -5668,7 +5668,7 @@ pub mod field {
}
}

impl<'a> Reader<'a> {
impl Reader<'_> {
pub fn reborrow(&self) -> Reader<'_> {
Self { ..*self }
}
Expand Down Expand Up @@ -9679,7 +9679,7 @@ pub mod type_ {
}
}

impl<'a> Reader<'a> {
impl Reader<'_> {
pub fn reborrow(&self) -> Reader<'_> {
Self { ..*self }
}
Expand Down Expand Up @@ -10044,7 +10044,7 @@ pub mod type_ {
}
}

impl<'a> Reader<'a> {
impl Reader<'_> {
pub fn reborrow(&self) -> Reader<'_> {
Self { ..*self }
}
Expand Down Expand Up @@ -10356,7 +10356,7 @@ pub mod type_ {
}
}

impl<'a> Reader<'a> {
impl Reader<'_> {
pub fn reborrow(&self) -> Reader<'_> {
Self { ..*self }
}
Expand Down Expand Up @@ -13069,7 +13069,7 @@ pub mod capnp_version {
}
}

impl<'a> Reader<'a> {
impl Reader<'_> {
pub fn reborrow(&self) -> Reader<'_> {
Self { ..*self }
}
Expand Down
20 changes: 19 additions & 1 deletion capnpc/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2052,6 +2052,7 @@ fn generate_node(
private_mod_interior.push(generate_members_by_discriminant(*node_reader)?);
private_mod_interior.push(generate_members_by_name(*node_reader)?);

let mut has_pointer_field = false;
let fields = struct_reader.get_fields()?;
for field in fields {
let name = get_field_name(field)?;
Expand All @@ -2060,6 +2061,19 @@ fn generate_node(
let discriminant_value = field.get_discriminant_value();
let is_union_field = discriminant_value != field::NO_DISCRIMINANT;

match field.which()? {
field::Slot(s) => match s.get_type()?.which()? {
type_::Text(())
| type_::Data(())
| type_::List(_)
| type_::Struct(_)
| type_::Interface(_)
| type_::AnyPointer(_) => has_pointer_field = true,
_ => (),
},
field::Group(_) => has_pointer_field = true,
}

if !is_union_field {
pipeline_impl_interior.push(generate_pipeline_getter(ctx, field)?);
let (ty, get, default_decl) = getter_text(ctx, &field, true, true)?;
Expand Down Expand Up @@ -2275,7 +2289,11 @@ fn generate_node(
]),
line("}"),
BlankLine,
Line(format!("impl <'a,{0}> Reader<'a,{0}> {1} {{", params.params, params.where_clause)),
if has_pointer_field { // we do this to keep clippy happy
Line(format!("impl <'a,{0}> Reader<'a,{0}> {1} {{", params.params, params.where_clause))
} else {
Line(format!("impl <{0}> Reader<'_,{0}> {1} {{", params.params, params.where_clause))
},
indent(vec![
Line(format!("pub fn reborrow(&self) -> Reader<'_,{}> {{",params.params)),
indent(line("Self { .. *self }")),
Expand Down

0 comments on commit 55ff380

Please sign in to comment.