Skip to content

Instantly share code, notes, and snippets.

@adria0
Created April 15, 2019 08:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adria0/5665dc4898f50b4c368af7310b5d8505 to your computer and use it in GitHub Desktop.
Save adria0/5665dc4898f50b4c368af7310b5d8505 to your computer and use it in GitHub Desktop.
lifetimed recursion
use std::cell::RefCell;
struct S<'a> {
value : RefCell<u64>,
next : Option<&'a Self>
}
impl<'a> S<'a> {
fn new(value: u64, next: Option<&'a Self>) -> Self {
S { value :RefCell::new(value), next }
}
fn zeroize(&'a self) {
*self.value.borrow_mut() = 0;
if let Some(next) = self.next {
next.zeroize()
}
}
}
fn main() {
let s0 = S::new(100,None);
let s1 = S::new(101,Some(&s0));
// use reborrow trick https://www.reddit.com/r/rust/comments/464jge/lifetime_issues_turning_tail_recursion_into_a_loop/d032cu0?utm_source=share&utm_medium=web2x
s1.zeroize();
s1.zeroize();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment