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
59
60
61
62
63
64
65
66
#![cfg_attr(feature = "nightly", feature(marker_trait_attr))]

pub mod stable_set;
//pub mod stable_map;

use std::{borrow::Borrow, ops::Deref};

mod impls;

pub struct Atom<'a, T: ?Sized> {
  __ptr: &'a T,
}

impl<'a, T: ?Sized> Atom<'a, T> {
  fn new(__ptr: &'a T) -> Self {
    Atom { __ptr }
  }

  pub fn as_ref(p: Self) -> &'a T {
    p.__ptr
  }
}

/**
  This trait guarantees that, for a type T: StablePointer,
  given a value `t: &'a T`:

    `&**t` shall live for at least `'a`

  i.e., when `*t` moves, the underlying `<T as Deref>::Target` does not move
*/
#[cfg_attr(feature = "nightly", marker)]
pub unsafe trait StablePointer:
  Deref + Borrow<<Self as Deref>::Target>
{
}

unsafe impl<T: ?Sized> StablePointer for Box<T> {}
unsafe impl<T: ?Sized> StablePointer for std::rc::Rc<T> {}
unsafe impl<T: ?Sized> StablePointer for std::sync::Arc<T> {}
unsafe impl<T> StablePointer for Vec<T> {}
unsafe impl StablePointer for String {}

pub trait AtomProxy<Owned>
  where Owned: Borrow<<Self as AtomProxy<Owned>>::Compare>
{
  type Compare: ?Sized + Ord;

  fn to_owned(&self) -> Owned;
  fn to_compare(&self) -> &Self::Compare;
}

impl<T> AtomProxy<T> for <T as Deref>::Target
where
  T: Deref + Borrow<<T as Deref>::Target>,
  <T as Deref>::Target: Ord + ToOwned<Owned = T>,
{
  type Compare = <T as Deref>::Target;

  fn to_owned(&self) -> T {
    <Self::Compare as ToOwned>::to_owned(self)
  }
  fn to_compare(&self) -> &Self {
    self
  }
}