Created
April 15, 2019 08:31
-
-
Save adria0/5665dc4898f50b4c368af7310b5d8505 to your computer and use it in GitHub Desktop.
lifetimed recursion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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