Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created June 18, 2019 11:45
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 rust-play/d3ae2723b7891447760bea0b54e2e3ec to your computer and use it in GitHub Desktop.
Save rust-play/d3ae2723b7891447760bea0b54e2e3ec to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
//use std::mem;
#[derive(PartialEq)]
enum Foo { A, B}
/*
use std::ops::Deref;
use std::ops::DerefMut;
impl Deref for Foo {
type Target = Foo;
fn deref(&self) -> &Self::Target {
panic!("not allowed");
}
}
impl DerefMut for Foo {
fn deref_mut(&mut self) -> &mut Self::Target {
panic!("not allowed");
}
}
static x: Foo = Foo::A;
static y: Foo = Foo::B;
*/
struct Bar {
x: (Foo, bool),
y: (Foo, bool)
}
impl Bar {
fn init() -> Self where Self: Sized {
Bar{x: (Foo::A, false), y: (Foo::B, false)}
}
fn get_x(&mut self) -> &mut Foo {
let (val, status) = &mut self.x;
if !*status {
*status = true
}
val
}
}
fn main() {
/*
let mut x: Foo = Foo::A;
let rx1: &mut Foo = &mut x;
*rx1 = Foo::B;
let rx2: &mut Foo = &mut x;
*rx2 = Foo::A;
*/
let mut bar = Bar::init();
let rx1: &mut Foo = bar.get_x();
*rx1 = Foo::B;
/* let rx2: &mut Foo = bar.get_x();
*rx2 = Foo::A;
assert!(*rx1 == *rx2)
*/}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment