Skip to content

Commit d1f567f

Browse files
authored
Fix lifetime warning in Rust 1.70 (#352)
* Fix lifetime warning in Rust 1.70 Starting with rustc 1.70.0, the code in raw.rs:hash_get_multi produced the following warnings: this `CString` is deallocated at the end of the statement, bind it to a variable to extend its lifetime While it would work in practice due to the underlying implementation just doing pointer juggling, this warning was correct: The CString created within the macro `f` would be destroyed before the reference to the pointer taken from it with `as_ptr` was used. Resolved by binding the lifetime of the CStrings to that of the iterator variable, which is alive until the function returns. * Proper fix this time (hopefully)
1 parent eefacd3 commit d1f567f

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

src/raw.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,11 @@ where
473473
{
474474
assert_eq!(fields.len(), values.len());
475475

476+
let fields = fields
477+
.iter()
478+
.map(|e| CString::new(e.clone()))
479+
.collect::<Result<Vec<CString>, _>>()?;
480+
476481
let mut fi = fields.iter();
477482
let mut vi = values.iter_mut();
478483

@@ -491,9 +496,7 @@ where
491496
}
492497
macro_rules! f {
493498
() => {
494-
CString::new((*fi.next().unwrap()).clone())
495-
.unwrap()
496-
.as_ptr()
499+
fi.next().unwrap().as_ptr()
497500
};
498501
}
499502
macro_rules! v {

0 commit comments

Comments
 (0)