use std::mem;
#[derive(Debug)]
struct S {
v: i32,
n: Option<Box<S>>,
}
struct I<'a> {
p: &'a mut Option<Box<S>>,
}
fn inplace<'a, 'b, T, F>(r: &'a mut &'b mut T, mut f: F)
where F: for<'c> FnMut(&'c mut T) -> &'c mut T {
unsafe {
let tmp_ref: *mut T = &mut **r;
let new_ref = f(&mut *tmp_ref);
*r = new_ref;
}
}
trait Animal {
fn kind(&self) -> &str;
}
struct Dog;
impl Animal for Dog {
fn kind(&self) -> &str { "dog" }
}
struct Cat;
impl Animal for Cat {
fn kind(&self) -> &str { "cat" }
}
fn what_kind1(a: &Animal) {
println!("{:?}", a.kind());
}
fn what_kind2<A: Animal>(a: &A) {
println!("{:?}", a.kind());
}
fn main() {
let a = Dog;
let b = Cat;
what_kind1(&a);
what_kind1(&b);
what_kind2(&a);
what_kind2(&b);
//let mut m = M{a: 42, b: 10};
//let mut n = Box::new(S{ v: 3, n: None });
//let mut n = Box::new(S{ v: 2, n: Some(n) });
//let mut n = Box::new(S{ v: 1, n: Some(n) });
//let mut l = Some(n);
//println!("{:#?}", l);
//{
//let mut i = I{ p: &mut l };
//let mut i = &mut i;
//loop {
//let mut stop = true;
//inplace(&mut i.p, |p| {
//if let &mut Some(ref mut node_box) = p {
//println!("-> {:?}", node_box.v);
//stop = false;
//&mut node_box.n
//} else {
//p
//}
//});
//if stop {
//break
//}
//}
//}
//println!("{:#?}", l);
//let mut a = Box::new(42);
//let mut cc = Box::new(21);
//let mut b = &mut *a;
//let mut c = &mut *cc;
//inplace(&mut &mut *b, |b| {
//c
//});
}