Skip to content
Myr

ptr.rs

This file

Path

src/ptr.rs

199 lines · 3.5 KiB · Rust

Last changed by

Merge pull request #178 from dtolnay/upDavid Tolnaybefore adoption3c586dd

History of this file

Take it

Merge pull request #178 from dtolnay/upDavid Tolnaybefore adoption3c586dd

line 1why, one step back
1use alloc::boxed::Box;

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

lines 2–5why, one step back
2use core::marker::PhantomData;
3use core::ptr::NonNull;45#[repr(transparent)]

Merge pull request #144 from dtolnay/ptrDavid Tolnaybefore adoptionce00418

lines 6–9why, one step back
6pub struct Own<T>
7where8 T: ?Sized,9{

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

lines 10–12why, one step back
10 pub ptr: NonNull<T>,
11}12

Merge pull request #144 from dtolnay/ptrDavid Tolnaybefore adoptionce00418

lines 13–22why, one step back
13unsafe impl<T> Send for Own<T> where T: ?Sized {}
1415unsafe impl<T> Sync for Own<T> where T: ?Sized {}1617impl<T> Copy for Own<T> where T: ?Sized {}1819impl<T> Clone for Own<T>20where21 T: ?Sized,22{

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

lines 23–27why, one step back
23 fn clone(&self) -> Self {
24 *self25 }26}27

Merge pull request #144 from dtolnay/ptrDavid Tolnaybefore adoptionce00418

lines 28–31why, one step back
28impl<T> Own<T>
29where30 T: ?Sized,31{

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

lines 32–37why, one step back
32 pub fn new(ptr: Box<T>) -> Self {
33 Own {34 ptr: unsafe { NonNull::new_unchecked(Box::into_raw(ptr)) },35 }36 }37

Merge pull request #137 from dtolnay/castDavid Tolnaybefore adoption3626541

line 38why, one step back
38 pub fn cast<U: CastTo>(self) -> Own<U::Target> {

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

lines 39–63why, one step back
39 Own {
40 ptr: self.ptr.cast(),41 }42 }4344 pub unsafe fn boxed(self) -> Box<T> {45 Box::from_raw(self.ptr.as_ptr())46 }4748 pub fn by_ref(&self) -> Ref<T> {49 Ref {50 ptr: self.ptr,51 lifetime: PhantomData,52 }53 }5455 pub fn by_mut(&mut self) -> Mut<T> {56 Mut {57 ptr: self.ptr,58 lifetime: PhantomData,59 }60 }61}6263#[repr(transparent)]

Merge pull request #144 from dtolnay/ptrDavid Tolnaybefore adoptionce00418

lines 64–67why, one step back
64pub struct Ref<'a, T>
65where66 T: ?Sized,67{

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

lines 68–71why, one step back
68 pub ptr: NonNull<T>,
69 lifetime: PhantomData<&'a T>,70}71

Merge pull request #144 from dtolnay/ptrDavid Tolnaybefore adoptionce00418

lines 72–77why, one step back
72impl<'a, T> Copy for Ref<'a, T> where T: ?Sized {}
7374impl<'a, T> Clone for Ref<'a, T>75where76 T: ?Sized,77{

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

lines 78–82why, one step back
78 fn clone(&self) -> Self {
79 *self80 }81}82

Merge pull request #144 from dtolnay/ptrDavid Tolnaybefore adoptionce00418

lines 83–86why, one step back
83impl<'a, T> Ref<'a, T>
84where85 T: ?Sized,86{

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

lines 87–93why, one step back
87 pub fn new(ptr: &'a T) -> Self {
88 Ref {89 ptr: NonNull::from(ptr),90 lifetime: PhantomData,91 }92 }93

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

lines 94–101why, one step back
94 #[cfg(not(anyhow_no_ptr_addr_of))]
95 pub fn from_raw(ptr: NonNull<T>) -> Self {96 Ref {97 ptr,98 lifetime: PhantomData,99 }100 }101

Merge pull request #137 from dtolnay/castDavid Tolnaybefore adoption3626541

line 102why, one step back
102 pub fn cast<U: CastTo>(self) -> Ref<'a, U::Target> {

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

lines 103–108why, one step back
103 Ref {
104 ptr: self.ptr.cast(),105 lifetime: PhantomData,106 }107 }108

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

lines 109–121why, one step back
109 #[cfg(not(anyhow_no_ptr_addr_of))]
110 pub fn by_mut(self) -> Mut<'a, T> {111 Mut {112 ptr: self.ptr,113 lifetime: PhantomData,114 }115 }116117 #[cfg(not(anyhow_no_ptr_addr_of))]118 pub fn as_ptr(self) -> *const T {119 self.ptr.as_ptr() as *const T120 }121

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

lines 122–127why, one step back
122 pub unsafe fn deref(self) -> &'a T {
123 &*self.ptr.as_ptr()124 }125}126127#[repr(transparent)]

Merge pull request #144 from dtolnay/ptrDavid Tolnaybefore adoptionce00418

lines 128–131why, one step back
128pub struct Mut<'a, T>
129where130 T: ?Sized,131{

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

lines 132–135why, one step back
132 pub ptr: NonNull<T>,
133 lifetime: PhantomData<&'a mut T>,134}135

Merge pull request #144 from dtolnay/ptrDavid Tolnaybefore adoptionce00418

lines 136–141why, one step back
136impl<'a, T> Copy for Mut<'a, T> where T: ?Sized {}
137138impl<'a, T> Clone for Mut<'a, T>139where140 T: ?Sized,141{

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

lines 142–146why, one step back
142 fn clone(&self) -> Self {
143 *self144 }145}146

Merge pull request #144 from dtolnay/ptrDavid Tolnaybefore adoptionce00418

lines 147–150why, one step back
147impl<'a, T> Mut<'a, T>
148where149 T: ?Sized,150{

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

line 151why, one step back
151 #[cfg(anyhow_no_ptr_addr_of)]

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

lines 152–158why, one step back
152 pub fn new(ptr: &'a mut T) -> Self {
153 Mut {154 ptr: NonNull::from(ptr),155 lifetime: PhantomData,156 }157 }158

Merge pull request #137 from dtolnay/castDavid Tolnaybefore adoption3626541

line 159why, one step back
159 pub fn cast<U: CastTo>(self) -> Mut<'a, U::Target> {

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

lines 160–161why, one step back
160 Mut {
161 ptr: self.ptr.cast(),

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

lines 162–169why, one step back
162 lifetime: PhantomData,
163 }164 }165166 #[cfg(not(anyhow_no_ptr_addr_of))]167 pub fn by_ref(self) -> Ref<'a, T> {168 Ref {169 ptr: self.ptr,

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

lines 170–183why, one step back
170 lifetime: PhantomData,
171 }172 }173174 pub fn extend<'b>(self) -> Mut<'b, T> {175 Mut {176 ptr: self.ptr,177 lifetime: PhantomData,178 }179 }180181 pub unsafe fn deref_mut(self) -> &'a mut T {182 &mut *self.ptr.as_ptr()183 }

Merge pull request #144 from dtolnay/ptrDavid Tolnaybefore adoptionce00418

line 184why, one step back
184}

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

line 185why, one step back
185

Merge pull request #144 from dtolnay/ptrDavid Tolnaybefore adoptionce00418

line 186why, one step back
186impl<'a, T> Mut<'a, T> {

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

lines 187–189why, one step back
187 pub unsafe fn read(self) -> T {
188 self.ptr.as_ptr().read()189 }

Merge pull request #137 from dtolnay/castDavid Tolnaybefore adoption3626541

lines 190–198why, one step back
190}
191192// Force turbofish on all calls of `.cast::<U>()`.193pub trait CastTo {194 type Target;195}196197impl<T> CastTo for T {198 type Target = T;

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

line 199why, one step back
199}