Skip to content

Commit 8a04b54

Browse files
d-e-s-odanielocfb
authored andcommitted
Stop eliding lifetimes
It's arguably confusing to see a definition such as ```rust impl<'a> OpenTproxySkel<'a> { pub fn maps(&self) -> OpenTproxyMaps { OpenTproxyMaps { inner: &self.obj } } } ``` because it omits the fact that the OpenTproxyMaps type actually has a lifetimes associated with it. Let's make sure to make lifetimes explicit to prevent such confusion from the start. Signed-off-by: Daniel Müller <[email protected]>
1 parent 71c8c02 commit 8a04b54

File tree

12 files changed

+88
-72
lines changed

12 files changed

+88
-72
lines changed

libbpf-cargo/src/gen/btf.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ impl<'s> GenBtf<'s> {
192192

193193
// All the non-bitfield fields have to be naturally aligned
194194
for m in composite.iter() {
195-
let align = self.type_by_id::<BtfType>(m.ty).unwrap().alignment()?;
195+
let align = self.type_by_id::<BtfType<'_>>(m.ty).unwrap().alignment()?;
196196

197197
if let MemberAttr::Normal { offset } = m.attr {
198198
if offset as usize % (align.get() * 8) != 0 {
@@ -250,7 +250,7 @@ impl<'s> GenBtf<'s> {
250250
///
251251
/// `ty` must be a struct, union, enum, or datasec type.
252252
pub fn type_definition(&self, ty: BtfType<'s>) -> Result<String> {
253-
let is_terminal = |ty: BtfType| -> bool {
253+
let is_terminal = |ty: BtfType<'_>| -> bool {
254254
matches!(
255255
ty.kind(),
256256
BtfKind::Void
@@ -308,7 +308,7 @@ impl<'s> GenBtf<'s> {
308308
&'a self,
309309
def: &mut String,
310310
dependent_types: &mut Vec<BtfType<'a>>,
311-
t: types::Composite,
311+
t: types::Composite<'_>,
312312
) -> Result<()> {
313313
let packed = self.is_struct_packed(&t)?;
314314

@@ -328,7 +328,7 @@ impl<'s> GenBtf<'s> {
328328
};
329329

330330
let field_ty = self
331-
.type_by_id::<BtfType>(member.ty)
331+
.type_by_id::<BtfType<'_>>(member.ty)
332332
.unwrap()
333333
.skip_mods_and_typedefs();
334334
if let Some(next_ty_id) = next_type(field_ty)? {
@@ -340,7 +340,7 @@ impl<'s> GenBtf<'s> {
340340
let padding = self.required_padding(
341341
offset,
342342
member_offset as usize / 8,
343-
&self.type_by_id::<BtfType>(member.ty).unwrap(),
343+
&self.type_by_id::<BtfType<'_>>(member.ty).unwrap(),
344344
packed,
345345
)?;
346346

@@ -352,7 +352,7 @@ impl<'s> GenBtf<'s> {
352352
));
353353
}
354354

355-
if let Some(ft) = self.type_by_id::<types::Array>(field_ty.type_id()) {
355+
if let Some(ft) = self.type_by_id::<types::Array<'_>>(field_ty.type_id()) {
356356
if ft.capacity() > 32 {
357357
gen_impl_default = true
358358
}
@@ -480,7 +480,7 @@ impl<'s> GenBtf<'s> {
480480
Ok(())
481481
}
482482

483-
fn type_definition_for_enums(&self, def: &mut String, t: types::Enum) -> Result<()> {
483+
fn type_definition_for_enums(&self, def: &mut String, t: types::Enum<'_>) -> Result<()> {
484484
let repr_size = match t.size() {
485485
1 => "8",
486486
2 => "16",
@@ -529,7 +529,7 @@ impl<'s> GenBtf<'s> {
529529
&'a self,
530530
def: &mut String,
531531
dependent_types: &mut Vec<BtfType<'a>>,
532-
t: types::DataSec,
532+
t: types::DataSec<'_>,
533533
) -> Result<()> {
534534
let mut sec_name = match t.name().map(|s| s.to_string_lossy()) {
535535
None => bail!("Datasec name is empty"),
@@ -545,7 +545,7 @@ impl<'s> GenBtf<'s> {
545545
let mut offset: u32 = 0;
546546
for datasec_var in t.iter() {
547547
let var = self
548-
.type_by_id::<types::Var>(datasec_var.ty)
548+
.type_by_id::<types::Var<'_>>(datasec_var.ty)
549549
.ok_or_else(|| anyhow!("BTF is invalid! Datasec var does not point to a var"))?;
550550

551551
if var.linkage() == Linkage::Static {

libbpf-cargo/src/gen/mod.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ fn gen_skel_map_defs(
300300
inner: &'a {mut_prefix}{inner_ty},
301301
}}
302302
303-
impl<'a> {struct_name}<'a> {{
303+
impl {struct_name}<'_> {{
304304
"#,
305305
)?;
306306

@@ -368,7 +368,7 @@ fn gen_skel_prog_defs(
368368
inner: &'a {mut_prefix}{inner_ty},
369369
}}
370370
371-
impl<'a> {struct_name}<'a> {{
371+
impl {struct_name}<'_> {{
372372
"#,
373373
)?;
374374

@@ -399,7 +399,7 @@ fn gen_skel_datasec_defs(skel: &mut String, obj_name: &str, object: &[u8]) -> Re
399399
};
400400
let btf = GenBtf::from(btf);
401401

402-
for ty in btf.type_by_kind::<types::DataSec>() {
402+
for ty in btf.type_by_kind::<types::DataSec<'_>>() {
403403
let name = match ty.name() {
404404
Some(s) => s.to_str()?,
405405
None => "",
@@ -451,7 +451,7 @@ fn gen_skel_map_getter(
451451
write!(
452452
skel,
453453
r#"
454-
pub fn {map_fn}(&{mut_prefix}self) -> {return_ty} {{
454+
pub fn {map_fn}(&{mut_prefix}self) -> {return_ty}<'_> {{
455455
{return_ty} {{
456456
inner: &{mut_prefix}self.obj,
457457
}}
@@ -488,7 +488,7 @@ fn gen_skel_prog_getter(
488488
write!(
489489
skel,
490490
r#"
491-
pub fn {prog_fn}(&{mut_prefix}self) -> {return_ty} {{
491+
pub fn {prog_fn}(&{mut_prefix}self) -> {return_ty}<'_> {{
492492
{return_ty} {{
493493
inner: &{mut_prefix}self.obj,
494494
}}
@@ -524,9 +524,9 @@ fn gen_skel_datasec_getters(
524524
write!(
525525
skel,
526526
r#"
527-
pub fn {name}(&mut self) -> &'a {mutability} {struct_name} {{
527+
pub fn {name}(&mut self) -> &'_ {mutability} {struct_name} {{
528528
unsafe {{
529-
std::mem::transmute::<*mut std::ffi::c_void, &'a {mutability} {struct_name}>(
529+
std::mem::transmute::<*mut std::ffi::c_void, &'_ {mutability} {struct_name}>(
530530
self.skel_config.map_mmap_ptr({idx}).unwrap()
531531
)
532532
}}
@@ -661,6 +661,7 @@ fn gen_skel_contents(_debug: bool, raw_obj_name: &str, obj_file_path: &Path) ->
661661
#[allow(non_camel_case_types)]
662662
#[allow(clippy::transmute_ptr_to_ref)]
663663
#[allow(clippy::upper_case_acronyms)]
664+
#[warn(single_use_lifetimes)]
664665
mod imp {{
665666
use libbpf_rs::libbpf_sys;
666667
use libbpf_rs::skel::OpenSkel;
@@ -785,7 +786,7 @@ fn gen_skel_contents(_debug: bool, raw_obj_name: &str, obj_file_path: &Path) ->
785786
}
786787
)?;
787788
writeln!(skel, "}}")?;
788-
writeln!(skel, "impl<'a> Open{name}Skel<'a> {{", name = &obj_name)?;
789+
writeln!(skel, "impl Open{name}Skel<'_> {{", name = &obj_name)?;
789790

790791
gen_skel_prog_getter(&mut skel, &mut object, &obj_name, true, false)?;
791792
gen_skel_prog_getter(&mut skel, &mut object, &obj_name, true, true)?;
@@ -815,10 +816,10 @@ fn gen_skel_contents(_debug: bool, raw_obj_name: &str, obj_file_path: &Path) ->
815816
r#"
816817
}}
817818
818-
unsafe impl<'a> Send for {name}Skel<'a> {{}}
819-
unsafe impl<'a> Sync for {name}Skel<'a> {{}}
819+
unsafe impl Send for {name}Skel<'_> {{}}
820+
unsafe impl Sync for {name}Skel<'_> {{}}
820821
821-
impl<'a> Skel for {name}Skel<'a> {{
822+
impl Skel for {name}Skel<'_> {{
822823
fn object(&self) -> &libbpf_rs::Object {{
823824
&self.obj
824825
}}
@@ -832,7 +833,7 @@ fn gen_skel_contents(_debug: bool, raw_obj_name: &str, obj_file_path: &Path) ->
832833
gen_skel_attach(&mut skel, &mut object, &obj_name)?;
833834
writeln!(skel, "}}")?;
834835

835-
write!(skel, "impl<'a> {name}Skel<'a> {{", name = &obj_name)?;
836+
write!(skel, "impl {name}Skel<'_> {{", name = &obj_name)?;
836837
gen_skel_prog_getter(&mut skel, &mut object, &obj_name, false, false)?;
837838
gen_skel_prog_getter(&mut skel, &mut object, &obj_name, false, true)?;
838839
gen_skel_map_getter(&mut skel, &mut object, &obj_name, false, false)?;
@@ -859,7 +860,7 @@ fn gen_skel(
859860
debug: bool,
860861
name: &str,
861862
obj: &Path,
862-
out: OutputDest,
863+
out: OutputDest<'_>,
863864
rustfmt_path: Option<&PathBuf>,
864865
) -> Result<()> {
865866
ensure!(!name.is_empty(), "Object file has no name");
@@ -936,7 +937,7 @@ pub fn gen_mods(objs: &[UnprocessedObj], rustfmt_path: Option<&PathBuf>) -> Resu
936937
pub fn gen_single(
937938
debug: bool,
938939
obj_file: &Path,
939-
output: OutputDest,
940+
output: OutputDest<'_>,
940941
rustfmt_path: Option<&PathBuf>,
941942
) -> Result<()> {
942943
let filename = match obj_file.file_name() {

libbpf-cargo/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@
5555
//! build`. This is a convenience command so you don't forget any steps. Alternatively, you could
5656
//! write a Makefile for your project.
5757
58-
#![warn(clippy::wildcard_imports)]
58+
#![warn(
59+
elided_lifetimes_in_paths,
60+
single_use_lifetimes,
61+
clippy::wildcard_imports
62+
)]
5963
#![deny(unsafe_op_in_unsafe_fn)]
6064

6165
use std::path::Path;

0 commit comments

Comments
 (0)