SHARE
TWEET

Untitled

a guest May 23rd, 2019 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. use std::cell::Cell;
  2.  
  3. struct Foo<'a> {
  4.     x: i32,
  5.  
  6.     /// y points to x
  7.     x_ref: Cell<Option<&'a i32>>
  8. }
  9.  
  10. impl<'a> Foo<'a> {
  11.     pub fn new(x: i32) -> Self {
  12.         Self { x, x_ref: Cell::new(None) }
  13.     }
  14.  
  15.     pub fn set(&'a self) {
  16.         self.x_ref.set(Some(&self.x));
  17.     }
  18.    
  19.     pub fn unset(&self) {
  20.         self.x_ref.take();
  21.     }
  22.  
  23.     fn x(&self) -> i32 {
  24.         self.x
  25.     }
  26.  
  27.     fn x_ref(&self) -> Option<&i32> {
  28.         self.x_ref.get()
  29.     }
  30. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top