Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::cell::Cell;
- struct Foo<'a> {
- x: i32,
- /// y points to x
- x_ref: Cell<Option<&'a i32>>
- }
- impl<'a> Foo<'a> {
- pub fn new(x: i32) -> Self {
- Self { x, x_ref: Cell::new(None) }
- }
- pub fn set(&'a self) {
- self.x_ref.set(Some(&self.x));
- }
- pub fn unset(&self) {
- self.x_ref.take();
- }
- fn x(&self) -> i32 {
- self.x
- }
- fn x_ref(&self) -> Option<&i32> {
- self.x_ref.get()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement