2 use core
::marker
::PhantomData
;
3 use core
::ptr
::NonNull
;
13 unsafe impl<T
> Send
for Own
<T
> where T
: ?Sized {}
15 unsafe impl<T
> Sync
for Own
<T
> where T
: ?Sized {}
17 impl<T
> Copy
for Own
<T
> where T
: ?Sized {}
19 impl<T
> Clone
for Own
<T
>
23 fn clone(&self) -> Self {
32 pub fn new(ptr
: Box
<T
>) -> Self {
34 ptr
: unsafe { NonNull::new_unchecked(Box::into_raw(ptr)) }
,
38 pub fn cast
<U
: CastTo
>(self) -> Own
<U
::Target
> {
44 pub unsafe fn boxed(self) -> Box
<T
> {
45 Box
::from_raw(self.ptr
.as_ptr())
48 pub fn by_ref(&self) -> Ref
<T
> {
51 lifetime
: PhantomData
,
55 pub fn by_mut(&mut self) -> Mut
<T
> {
58 lifetime
: PhantomData
,
69 lifetime
: PhantomData
<&'a T
>,
72 impl<'a
, T
> Copy
for Ref
<'a
, T
> where T
: ?Sized {}
74 impl<'a
, T
> Clone
for Ref
<'a
, T
>
78 fn clone(&self) -> Self {
83 impl<'a
, T
> Ref
<'a
, T
>
87 pub fn new(ptr
: &'a T
) -> Self {
89 ptr
: NonNull
::from(ptr
),
90 lifetime
: PhantomData
,
94 #[cfg(not(anyhow_no_ptr_addr_of))]
95 pub fn from_raw(ptr
: NonNull
<T
>) -> Self {
98 lifetime
: PhantomData
,
102 pub fn cast
<U
: CastTo
>(self) -> Ref
<'a
, U
::Target
> {
104 ptr
: self.ptr
.cast(),
105 lifetime
: PhantomData
,
109 #[cfg(not(anyhow_no_ptr_addr_of))]
110 pub fn by_mut(self) -> Mut
<'a
, T
> {
113 lifetime
: PhantomData
,
117 #[cfg(not(anyhow_no_ptr_addr_of))]
118 pub fn as_ptr(self) -> *const T
{
119 self.ptr
.as_ptr() as *const T
122 pub unsafe fn deref(self) -> &'a T
{
128 pub struct Mut
<'a
, T
>
133 lifetime
: PhantomData
<&'a
mut T
>,
136 impl<'a
, T
> Copy
for Mut
<'a
, T
> where T
: ?Sized {}
138 impl<'a
, T
> Clone
for Mut
<'a
, T
>
142 fn clone(&self) -> Self {
147 impl<'a
, T
> Mut
<'a
, T
>
151 #[cfg(anyhow_no_ptr_addr_of)]
152 pub fn new(ptr
: &'a
mut T
) -> Self {
154 ptr
: NonNull
::from(ptr
),
155 lifetime
: PhantomData
,
159 pub fn cast
<U
: CastTo
>(self) -> Mut
<'a
, U
::Target
> {
161 ptr
: self.ptr
.cast(),
162 lifetime
: PhantomData
,
166 #[cfg(not(anyhow_no_ptr_addr_of))]
167 pub fn by_ref(self) -> Ref
<'a
, T
> {
170 lifetime
: PhantomData
,
174 pub fn extend
<'b
>(self) -> Mut
<'b
, T
> {
177 lifetime
: PhantomData
,
181 pub unsafe fn deref_mut(self) -> &'a
mut T
{
182 &mut *self.ptr
.as_ptr()
186 impl<'a
, T
> Mut
<'a
, T
> {
187 pub unsafe fn read(self) -> T
{
188 self.ptr
.as_ptr().read()
192 // Force turbofish on all calls of `.cast::<U>()`.
197 impl<T
> CastTo
for T
{