protobuf/
owning_ref.rs

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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//! Utility similar to provided by `owning_ref` crate.

use std::fmt;
use std::fmt::Debug;
use std::ops::Deref;
use std::sync::Arc;

enum Owner<A: 'static> {
    Arc(Arc<A>),
    Static(&'static A),
}

impl<A: 'static> Deref for Owner<A> {
    type Target = A;

    fn deref(&self) -> &A {
        match self {
            Owner::Arc(a) => &*a,
            Owner::Static(a) => a,
        }
    }
}

pub(crate) struct OwningRef<A: 'static, B: 'static> {
    owner: Owner<A>,
    ptr: *const B,
}

unsafe impl<A: Send + Sync + 'static, B: Send + Sync + 'static> Sync for OwningRef<A, B> {}
unsafe impl<A: Send + Sync + 'static, B: Send + Sync + 'static> Send for OwningRef<A, B> {}

impl<A: 'static, B: 'static> Deref for OwningRef<A, B> {
    type Target = B;

    fn deref(&self) -> &B {
        // SAFETY: `self.owner` owns the data and it is not movable.
        unsafe { &*self.ptr }
    }
}

impl<A: 'static> Clone for Owner<A> {
    fn clone(&self) -> Owner<A> {
        match self {
            Owner::Arc(arc) => Owner::Arc(arc.clone()),
            Owner::Static(ptr) => Owner::Static(ptr),
        }
    }
}

impl<A: 'static, B: 'static> Clone for OwningRef<A, B> {
    fn clone(&self) -> OwningRef<A, B> {
        OwningRef {
            ptr: self.ptr,
            owner: self.owner.clone(),
        }
    }
}

impl<A: 'static, B: fmt::Debug + 'static> Debug for OwningRef<A, B> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        Debug::fmt(&**self, f)
    }
}

impl<A: 'static> OwningRef<A, A> {
    pub(crate) fn new_arc(arc: Arc<A>) -> OwningRef<A, A> {
        OwningRef {
            ptr: Arc::as_ptr(&arc),
            owner: Owner::Arc(arc),
        }
    }

    pub(crate) fn new_static(ptr: &'static A) -> OwningRef<A, A> {
        OwningRef {
            ptr,
            owner: Owner::Static(ptr),
        }
    }

    pub(crate) fn owner(&self) -> &A {
        &self.owner
    }
}

impl<A: 'static, B: 'static> OwningRef<A, B> {
    pub(crate) fn _map<C>(self, f: impl FnOnce(&B) -> &C) -> OwningRef<A, C> {
        let ptr = f(&*self);
        OwningRef {
            ptr,
            owner: self.owner,
        }
    }

    pub(crate) fn flat_map_slice<'x, C, T: FnOnce(&B) -> &[C]>(
        &self,
        f: T,
    ) -> impl Iterator<Item = OwningRef<A, C>> + '_
    where
        C: 'static,
    {
        f(&self).into_iter().map(|ptr| OwningRef {
            ptr,
            owner: self.owner.clone(),
        })
    }
}