Coverage Report

Created: 2021-03-22 08:29

/rust/registry/src/github.com-1ecc6299db9ec823/anyhow-1.0.39/src/ptr.rs
Line
Count
Source
1
use crate::alloc::Box;
2
use core::marker::PhantomData;
3
use core::ptr::NonNull;
4
5
#[repr(transparent)]
6
pub struct Own<T> {
7
    pub ptr: NonNull<T>,
8
}
9
10
unsafe impl<T> Send for Own<T> {}
11
unsafe impl<T> Sync for Own<T> {}
12
impl<T> Copy for Own<T> {}
13
impl<T> Clone for Own<T> {
14
    fn clone(&self) -> Self {
15
        *self
16
    }
17
}
18
19
impl<T> Own<T> {
20
44.4k
    pub fn new(ptr: Box<T>) -> Self {
21
44.4k
        Own {
22
44.4k
            ptr: unsafe { NonNull::new_unchecked(Box::into_raw(ptr)) },
23
44.4k
        }
24
44.4k
    }
<anyhow::ptr::Own<anyhow::error::ErrorImpl<anyhow::wrapper::MessageError<&str>>>>::new
Line
Count
Source
20
663
    pub fn new(ptr: Box<T>) -> Self {
21
663
        Own {
22
663
            ptr: unsafe { NonNull::new_unchecked(Box::into_raw(ptr)) },
23
663
        }
24
663
    }
<anyhow::ptr::Own<anyhow::error::ErrorImpl<wasmparser::primitives::BinaryReaderError>>>::new
Line
Count
Source
20
42.8k
    pub fn new(ptr: Box<T>) -> Self {
21
42.8k
        Own {
22
42.8k
            ptr: unsafe { NonNull::new_unchecked(Box::into_raw(ptr)) },
23
42.8k
        }
24
42.8k
    }
<anyhow::ptr::Own<anyhow::error::ErrorImpl<anyhow::wrapper::MessageError<alloc::string::String>>>>::new
Line
Count
Source
20
950
    pub fn new(ptr: Box<T>) -> Self {
21
950
        Own {
22
950
            ptr: unsafe { NonNull::new_unchecked(Box::into_raw(ptr)) },
23
950
        }
24
950
    }
25
26
88.9k
    pub fn cast<U: CastTo>(self) -> Own<U::Target> {
27
88.9k
        Own {
28
88.9k
            ptr: self.ptr.cast(),
29
88.9k
        }
30
88.9k
    }
<anyhow::ptr::Own<anyhow::error::ErrorImpl>>::cast::<anyhow::error::ErrorImpl<anyhow::wrapper::MessageError<&str>>>
Line
Count
Source
26
663
    pub fn cast<U: CastTo>(self) -> Own<U::Target> {
27
663
        Own {
28
663
            ptr: self.ptr.cast(),
29
663
        }
30
663
    }
<anyhow::ptr::Own<anyhow::error::ErrorImpl<anyhow::wrapper::MessageError<&str>>>>::cast::<anyhow::error::ErrorImpl>
Line
Count
Source
26
663
    pub fn cast<U: CastTo>(self) -> Own<U::Target> {
27
663
        Own {
28
663
            ptr: self.ptr.cast(),
29
663
        }
30
663
    }
<anyhow::ptr::Own<anyhow::error::ErrorImpl>>::cast::<anyhow::error::ErrorImpl<wasmparser::primitives::BinaryReaderError>>
Line
Count
Source
26
42.8k
    pub fn cast<U: CastTo>(self) -> Own<U::Target> {
27
42.8k
        Own {
28
42.8k
            ptr: self.ptr.cast(),
29
42.8k
        }
30
42.8k
    }
<anyhow::ptr::Own<anyhow::error::ErrorImpl<wasmparser::primitives::BinaryReaderError>>>::cast::<anyhow::error::ErrorImpl>
Line
Count
Source
26
42.8k
    pub fn cast<U: CastTo>(self) -> Own<U::Target> {
27
42.8k
        Own {
28
42.8k
            ptr: self.ptr.cast(),
29
42.8k
        }
30
42.8k
    }
<anyhow::ptr::Own<anyhow::error::ErrorImpl>>::cast::<anyhow::error::ErrorImpl<anyhow::wrapper::MessageError<alloc::string::String>>>
Line
Count
Source
26
950
    pub fn cast<U: CastTo>(self) -> Own<U::Target> {
27
950
        Own {
28
950
            ptr: self.ptr.cast(),
29
950
        }
30
950
    }
<anyhow::ptr::Own<anyhow::error::ErrorImpl<anyhow::wrapper::MessageError<alloc::string::String>>>>::cast::<anyhow::error::ErrorImpl>
Line
Count
Source
26
950
    pub fn cast<U: CastTo>(self) -> Own<U::Target> {
27
950
        Own {
28
950
            ptr: self.ptr.cast(),
29
950
        }
30
950
    }
31
32
44.4k
    pub unsafe fn boxed(self) -> Box<T> {
33
44.4k
        Box::from_raw(self.ptr.as_ptr())
34
44.4k
    }
<anyhow::ptr::Own<anyhow::error::ErrorImpl<anyhow::wrapper::MessageError<alloc::string::String>>>>::boxed
Line
Count
Source
32
950
    pub unsafe fn boxed(self) -> Box<T> {
33
950
        Box::from_raw(self.ptr.as_ptr())
34
950
    }
<anyhow::ptr::Own<anyhow::error::ErrorImpl<wasmparser::primitives::BinaryReaderError>>>::boxed
Line
Count
Source
32
42.8k
    pub unsafe fn boxed(self) -> Box<T> {
33
42.8k
        Box::from_raw(self.ptr.as_ptr())
34
42.8k
    }
<anyhow::ptr::Own<anyhow::error::ErrorImpl<anyhow::wrapper::MessageError<&str>>>>::boxed
Line
Count
Source
32
663
    pub unsafe fn boxed(self) -> Box<T> {
33
663
        Box::from_raw(self.ptr.as_ptr())
34
663
    }
35
36
    pub fn by_ref(&self) -> Ref<T> {
37
        Ref {
38
            ptr: self.ptr,
39
            lifetime: PhantomData,
40
        }
41
    }
42
43
    pub fn by_mut(&mut self) -> Mut<T> {
44
        Mut {
45
            ptr: self.ptr,
46
            lifetime: PhantomData,
47
        }
48
    }
49
}
50
51
#[repr(transparent)]
52
pub struct Ref<'a, T> {
53
    pub ptr: NonNull<T>,
54
    lifetime: PhantomData<&'a T>,
55
}
56
57
impl<'a, T> Copy for Ref<'a, T> {}
58
impl<'a, T> Clone for Ref<'a, T> {
59
    fn clone(&self) -> Self {
60
        *self
61
    }
62
}
63
64
impl<'a, T> Ref<'a, T> {
65
    pub fn new(ptr: &'a T) -> Self {
66
        Ref {
67
            ptr: NonNull::from(ptr),
68
            lifetime: PhantomData,
69
        }
70
    }
71
72
    pub fn cast<U: CastTo>(self) -> Ref<'a, U::Target> {
73
        Ref {
74
            ptr: self.ptr.cast(),
75
            lifetime: PhantomData,
76
        }
77
    }
78
79
    pub unsafe fn deref(self) -> &'a T {
80
        &*self.ptr.as_ptr()
81
    }
82
}
83
84
#[repr(transparent)]
85
pub struct Mut<'a, T> {
86
    pub ptr: NonNull<T>,
87
    lifetime: PhantomData<&'a mut T>,
88
}
89
90
impl<'a, T> Copy for Mut<'a, T> {}
91
impl<'a, T> Clone for Mut<'a, T> {
92
    fn clone(&self) -> Self {
93
        *self
94
    }
95
}
96
97
impl<'a, T> Mut<'a, T> {
98
    pub fn new(ptr: &'a mut T) -> Self {
99
        Mut {
100
            ptr: NonNull::from(ptr),
101
            lifetime: PhantomData,
102
        }
103
    }
104
105
    pub fn cast<U: CastTo>(self) -> Mut<'a, U::Target> {
106
        Mut {
107
            ptr: self.ptr.cast(),
108
            lifetime: PhantomData,
109
        }
110
    }
111
112
    pub fn extend<'b>(self) -> Mut<'b, T> {
113
        Mut {
114
            ptr: self.ptr,
115
            lifetime: PhantomData,
116
        }
117
    }
118
119
    pub unsafe fn deref_mut(self) -> &'a mut T {
120
        &mut *self.ptr.as_ptr()
121
    }
122
123
    pub unsafe fn read(self) -> T {
124
        self.ptr.as_ptr().read()
125
    }
126
}
127
128
// Force turbofish on all calls of `.cast::<U>()`.
129
pub trait CastTo {
130
    type Target;
131
}
132
133
impl<T> CastTo for T {
134
    type Target = T;
135
}