summary refs log tree commit diff stats
path: root/tests/movable.rs
blob: f70297930e9338cb8355ee6d71f9d1d8bf3ad77e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//! Tests of "moving" a holder.
//!
//! We cannot move the actual holders themselves but we can move owned handles
//! (Box, Rc, etc) to them.

use std::cell::Cell;
use std::rc::Rc;

use selfref::Holder;
use selfref::opaque;

struct Foo<'this> {
    foo: Cell<Option<&'this Foo<'this>>>,
}

struct FooKey;

opaque! {
    impl Opaque for FooKey {
        type Kind<'this> = Foo<'this>;
    }
}

fn create_holder() -> Holder<'static, FooKey> {
    Holder::new_with(|builder| builder.build(Foo {
        foo: Cell::new(None),
    }))
}

#[test]
fn move_box() {
    // this is UB without the !Unpin hack
    // same goes for any pinned container that has &mut's to it.
    let b1 = Box::pin(create_holder());
    b1.as_ref().operate_in(|foo| {
        foo.foo.set(Some(foo.get_ref()));
    });
    let b2 = b1;
    b2.as_ref().operate_in(|foo| {
        // check that we can access the self-reference
        assert!(foo.foo.get().unwrap().foo.get().is_some());
    });
}

#[test]
fn move_rc() {
    // this is perfectly fine without the !Unpin hack!
    // any container that behaves like a shared ref is fine for this.
    let b1 = Rc::pin(create_holder());
    b1.as_ref().operate_in(|foo| {
        foo.foo.set(Some(foo.get_ref()));
    });
    let b2 = b1;
    b2.as_ref().operate_in(|foo| {
        // check that we can access the self-reference
        assert!(foo.foo.get().unwrap().foo.get().is_some());
    });
}