@@ -43,57 +43,86 @@ pub struct OwningPtr<'a>(NonNull<u8>, PhantomData<&'a mut u8>);
43
43
macro_rules! impl_ptr {
44
44
( $ptr: ident) => {
45
45
impl $ptr<' _> {
46
+ /// Calculates the offset from a pointer.
47
+ /// As the pointer is type-erased, there is no size information available. The provided
48
+ /// `count` parameter is in raw bytes.
49
+ ///
50
+ /// *See also: [`ptr::offset`][ptr_offset]*
51
+ ///
46
52
/// # Safety
47
53
/// the offset cannot make the existing ptr null, or take it out of bounds for its allocation.
54
+ ///
55
+ /// [ptr_offset]: https://doc.rust-lang.org/std/primitive.pointer.html#method.offset
48
56
#[ inline]
49
57
pub unsafe fn offset( self , count: isize ) -> Self {
50
58
Self (
51
- NonNull :: new_unchecked( self . 0 . as_ptr( ) . offset( count) ) ,
59
+ NonNull :: new_unchecked( self . as_ptr( ) . offset( count) ) ,
52
60
PhantomData ,
53
61
)
54
62
}
55
63
64
+ /// Calculates the offset from a pointer (convenience for `.offset(count as isize)`).
65
+ /// As the pointer is type-erased, there is no size information available. The provided
66
+ /// `count` parameter is in raw bytes.
67
+ ///
68
+ /// *See also: [`ptr::add`][ptr_add]*
69
+ ///
56
70
/// # Safety
57
71
/// the offset cannot make the existing ptr null, or take it out of bounds for its allocation.
72
+ ///
73
+ /// [ptr_add]: https://doc.rust-lang.org/std/primitive.pointer.html#method.add
58
74
#[ inline]
59
75
pub unsafe fn add( self , count: usize ) -> Self {
60
76
Self (
61
- NonNull :: new_unchecked( self . 0 . as_ptr( ) . add( count) ) ,
77
+ NonNull :: new_unchecked( self . as_ptr( ) . add( count) ) ,
62
78
PhantomData ,
63
79
)
64
80
}
65
81
66
- /// # Safety
82
+ /// Creates a new instance from a raw pointer.
67
83
///
84
+ /// # Safety
68
85
/// The lifetime for the returned item must not exceed the lifetime `inner` is valid for
69
86
#[ inline]
70
87
pub unsafe fn new( inner: NonNull <u8 >) -> Self {
71
88
Self ( inner, PhantomData )
72
89
}
73
-
74
- #[ inline]
75
- pub fn inner( & self ) -> NonNull <u8 > {
76
- self . 0
77
- }
78
90
}
79
91
} ;
80
92
}
81
93
82
94
impl_ptr ! ( Ptr ) ;
83
95
impl < ' a > Ptr < ' a > {
84
- /// # Safety
96
+ /// Transforms this [`Ptr`] into an [`PtrMut`]
85
97
///
98
+ /// # Safety
86
99
/// Another [`PtrMut`] for the same [`Ptr`] must not be created until the first is dropped.
87
100
#[ inline]
88
101
pub unsafe fn assert_unique ( self ) -> PtrMut < ' a > {
89
102
PtrMut ( self . 0 , PhantomData )
90
103
}
91
104
105
+ /// Transforms this [`Ptr<T>`] into a `&T` with the same lifetime
106
+ ///
92
107
/// # Safety
93
108
/// Must point to a valid `T`
94
109
#[ inline]
95
110
pub unsafe fn deref < T > ( self ) -> & ' a T {
96
- & * self . 0 . as_ptr ( ) . cast ( )
111
+ & * self . as_ptr ( ) . cast ( )
112
+ }
113
+
114
+ /// Gets the underlying pointer, erasing the associated lifetime.
115
+ ///
116
+ /// If possible, it is strongly encouraged to use [`deref`](Self::deref) over this function,
117
+ /// as it retains the lifetime.
118
+ ///
119
+ /// # Safety
120
+ /// All subsequent operations to the returned pointer must be valid inside the
121
+ /// associated lifetime.
122
+ #[ inline]
123
+ #[ allow( clippy:: wrong_self_convention) ]
124
+ pub unsafe fn as_ptr ( self ) -> * mut u8 {
125
+ self . 0 . as_ptr ( )
97
126
}
98
127
}
99
128
impl_ptr ! ( PtrMut ) ;
@@ -113,7 +142,21 @@ impl<'a> PtrMut<'a> {
113
142
/// Must point to a valid `T`
114
143
#[ inline]
115
144
pub unsafe fn deref_mut < T > ( self ) -> & ' a mut T {
116
- & mut * self . inner ( ) . as_ptr ( ) . cast ( )
145
+ & mut * self . as_ptr ( ) . cast ( )
146
+ }
147
+
148
+ /// Gets the underlying pointer, erasing the associated lifetime.
149
+ ///
150
+ /// If possible, it is strongly encouraged to use [`deref_mut`](Self::deref_mut) over
151
+ /// this function, as it retains the lifetime.
152
+ ///
153
+ /// # Safety
154
+ /// All subsequent operations to the returned pointer must be valid inside the
155
+ /// associated lifetime.
156
+ #[ inline]
157
+ #[ allow( clippy:: wrong_self_convention) ]
158
+ pub unsafe fn as_ptr ( self ) -> * mut u8 {
159
+ self . 0 . as_ptr ( )
117
160
}
118
161
}
119
162
impl_ptr ! ( OwningPtr ) ;
@@ -132,7 +175,30 @@ impl<'a> OwningPtr<'a> {
132
175
/// Must point to a valid `T`.
133
176
#[ inline]
134
177
pub unsafe fn read < T > ( self ) -> T {
135
- self . inner ( ) . as_ptr ( ) . cast :: < T > ( ) . read ( )
178
+ self . as_ptr ( ) . cast :: < T > ( ) . read ( )
179
+ }
180
+
181
+ //// Consumes the [`OwningPtr`] to drop the underlying data of type `T`.
182
+ ///
183
+ /// # Safety
184
+ /// Must point to a valid `T`.
185
+ #[ inline]
186
+ pub unsafe fn drop_as < T > ( self ) {
187
+ self . as_ptr ( ) . cast :: < T > ( ) . drop_in_place ( )
188
+ }
189
+
190
+ /// Gets the underlying pointer, erasing the associated lifetime.
191
+ ///
192
+ /// If possible, it is strongly encouraged to use the other more type-safe functions
193
+ /// over this function.
194
+ ///
195
+ /// # Safety
196
+ /// All subsequent operations to the returned pointer must be valid inside the
197
+ /// associated lifetime.
198
+ #[ inline]
199
+ #[ allow( clippy:: wrong_self_convention) ]
200
+ pub unsafe fn as_ptr ( self ) -> * mut u8 {
201
+ self . 0 . as_ptr ( )
136
202
}
137
203
}
138
204
0 commit comments