Debian Codegraph @

src/lib.rs

raw symbols SCIP
1 // Copyright 2016-2019 bluss and rawpointer developers.
2 //
3 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6 // option. This file may not be copied, modified, or distributed
7 // except according to those terms.
8 #![no_std]
9
10
11 //! Rawpointer adds extra utility methods to raw pointers `*const T`, `*mut T`
12 //! and `NonNull<T>`.
13 //!
14 //! Features include:
15 //!
16 //! - Strided offsets - [`.stride_offset(stride,
17 //! index)`](PointerExt::stride_offset) make it easy to compute
18 //! pointer offsets where the index is unsigned and the stride is signed.
19 //!
20 //! - Offsetting methods in general for `NonNull`, since it does not have these
21 //! from libcore
22 //!
23 //! - Post- and preincrement and post- and predecrement methods
24 //!
25 //! - For `p++` use [`p.post_inc()`](PointerExt::post_inc).
26 //! - For `++p` use [`p.pre_inc()`](PointerExt::pre_inc).
27 //! - For `p--` use [`p.post_dec()`](PointerExt::post_dec).
28 //! - For `--p` use [`p.pre_dec()`](PointerExt::pre_dec).
29 //!
30 //! ```rust
31 //! use rawpointer::PointerExt;
32 //!
33 //! unsafe {
34 //! // In this example:
35 //! // Use .post_inc() to iterate and overwrite the first four
36 //! // elements of the array.
37 //!
38 //! let mut xs = [0; 16];
39 //! let mut ptr = xs.as_mut_ptr();
40 //! let end = ptr.offset(4);
41 //! let mut i = 0;
42 //! while ptr != end {
43 //! *ptr.post_inc() = i;
44 //! i += 1;
45 //! }
46 //! assert_eq!(&xs[..8], &[0, 1, 2, 3, 0, 0, 0, 0]);
47 //! }
48 //! ```
49 //!
50 //! ## Safety
51 //!
52 //! See the Rust [core::ptr] documentation for more information.
53 //!
54 //! ## Rust Version
55 //!
56 //! This version of the crate requires Rust 1.26 or later
57
58 use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().;
59 use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/::NonNullrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/non_null/NonNull#;
60
61 /// Return the number of elements of `T` from `start` to `end`.<br>
62 /// Return the arithmetic difference if `T` is zero size.
63 #[inline(always)]
64 pub fn ptrdistancerust-analyzer cargo rawpointer 0.2.1 ptrdistance().<T>(start: *const T, end: *const T) -> usize {
65 let size = size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<T>();
66 if size rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().0 {
67 (end as usize).wrapping_subrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]wrapping_sub().(start as usize)
68 } else {
69 (end as usize rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().start as usize) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div().size
70 }
71 }
72
73 /// Extension methods for raw pointers
74 pub trait PointerExtrust-analyzer cargo rawpointer 0.2.1 PointerExt# : Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy# {
75 unsafe fn offsetrust-analyzer cargo rawpointer 0.2.1 PointerExt#offset().(self, i: isize) -> Self;
76
77 unsafe fn addrust-analyzer cargo rawpointer 0.2.1 PointerExt#add().(self, i: usize) -> Self {
78 self.offsetrust-analyzer cargo rawpointer 0.2.1 PointerExt#offset().(i as isize)
79 }
80
81 unsafe fn subrust-analyzer cargo rawpointer 0.2.1 PointerExt#sub().(self, i: usize) -> Self {
82 self.offsetrust-analyzer cargo rawpointer 0.2.1 PointerExt#offset().((i as isize).wrapping_negrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[isize]wrapping_neg().())
83 }
84
85 /// Increment the pointer by 1, and return its new value.
86 ///
87 /// Equivalent to the C idiom `++ptr`.
88 #[inline(always)]
89 unsafe fn pre_incrust-analyzer cargo rawpointer 0.2.1 PointerExt#pre_inc().(&mut self) -> Self {
90 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().self = self.offsetrust-analyzer cargo rawpointer 0.2.1 PointerExt#offset().(1);
91 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().self
92 }
93
94 /// Increment the pointer by 1, but return its old value.
95 ///
96 /// Equivalent to the C idiom `ptr++`.
97 #[inline(always)]
98 unsafe fn post_incrust-analyzer cargo rawpointer 0.2.1 PointerExt#post_inc().(&mut self) -> Self {
99 let current = *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().self;
100 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().self = self.offsetrust-analyzer cargo rawpointer 0.2.1 PointerExt#offset().(1);
101 current
102 }
103
104 /// Decrement the pointer by 1, and return its new value.
105 ///
106 /// Equivalent to the C idiom `--ptr`.
107 #[inline(always)]
108 unsafe fn pre_decrust-analyzer cargo rawpointer 0.2.1 PointerExt#pre_dec().(&mut self) -> Self {
109 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().self = self.offsetrust-analyzer cargo rawpointer 0.2.1 PointerExt#offset().(-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[isize][Neg]neg().1);
110 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().self
111 }
112
113 /// Decrement the pointer by 1, but return its old value.
114 ///
115 /// Equivalent to the C idiom `ptr--`.
116 #[inline(always)]
117 unsafe fn post_decrust-analyzer cargo rawpointer 0.2.1 PointerExt#post_dec().(&mut self) -> Self {
118 let current = *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().self;
119 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().self = self.offsetrust-analyzer cargo rawpointer 0.2.1 PointerExt#offset().(-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[isize][Neg]neg().1);
120 current
121 }
122
123 /// Increment by 1
124 #[inline(always)]
125 unsafe fn incrust-analyzer cargo rawpointer 0.2.1 PointerExt#inc().(&mut self) {
126 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().self = self.offsetrust-analyzer cargo rawpointer 0.2.1 PointerExt#offset().(1);
127 }
128
129 /// Decrement by 1
130 #[inline(always)]
131 unsafe fn decrust-analyzer cargo rawpointer 0.2.1 PointerExt#dec().(&mut self) {
132 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().self = self.offsetrust-analyzer cargo rawpointer 0.2.1 PointerExt#offset().(-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[isize][Neg]neg().1);
133 }
134
135 /// Offset the pointer by `s` multiplied by `index`.
136 #[inline(always)]
137 unsafe fn stride_offsetrust-analyzer cargo rawpointer 0.2.1 PointerExt#stride_offset().(self, s: isize, index: usize) -> Self {
138 self.offsetrust-analyzer cargo rawpointer 0.2.1 PointerExt#offset().(s rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[isize][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[isize][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[isize][`Mul<Self>`]mul().index as isize)
139 }
140 }
141
142 impl<T> PointerExtrust-analyzer cargo rawpointer 0.2.1 PointerExt# for *const T {
143 #[inline(always)]
144 unsafe fn offsetrust-analyzer cargo rawpointer 0.2.1 impl#[`*const T`][PointerExt]offset().(self, i: isize) -> Selfrust-analyzer cargo rawpointer 0.2.1 impl#[`*const T`][PointerExt] {
145 self.offsetrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/const_ptr/impl#[`*const T`]offset().(i)
146 }
147
148 // Call inherent version of add/sub
149 #[inline]
150 unsafe fn addrust-analyzer cargo rawpointer 0.2.1 impl#[`*const T`][PointerExt]add().(self, i: usize) -> Selfrust-analyzer cargo rawpointer 0.2.1 impl#[`*const T`][PointerExt] {
151 self.addrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/const_ptr/impl#[`*const T`]add().(i)
152 }
153
154 #[inline]
155 unsafe fn subrust-analyzer cargo rawpointer 0.2.1 impl#[`*const T`][PointerExt]sub().(self, i: usize) -> Selfrust-analyzer cargo rawpointer 0.2.1 impl#[`*const T`][PointerExt] {
156 self.subrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/const_ptr/impl#[`*const T`]sub().(i)
157 }
158 }
159
160 impl<T> PointerExtrust-analyzer cargo rawpointer 0.2.1 PointerExt# for *mut T {
161 #[inline(always)]
162 unsafe fn offsetrust-analyzer cargo rawpointer 0.2.1 impl#[`*mut T`][PointerExt]offset().(self, i: isize) -> Selfrust-analyzer cargo rawpointer 0.2.1 impl#[`*mut T`][PointerExt] {
163 self.offsetrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]offset().(i)
164 }
165
166 #[inline]
167 unsafe fn addrust-analyzer cargo rawpointer 0.2.1 impl#[`*mut T`][PointerExt]add().(self, i: usize) -> Selfrust-analyzer cargo rawpointer 0.2.1 impl#[`*mut T`][PointerExt] {
168 self.addrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]add().(i)
169 }
170
171 #[inline]
172 unsafe fn subrust-analyzer cargo rawpointer 0.2.1 impl#[`*mut T`][PointerExt]sub().(self, i: usize) -> Selfrust-analyzer cargo rawpointer 0.2.1 impl#[`*mut T`][PointerExt] {
173 self.subrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]sub().(i)
174 }
175 }
176
177 /// `NonNull<T>` supports the same offsetting methods under the same
178 /// safety constraints as the other raw pointer implementations.
179 ///
180 /// There is no difference - both when offsetting `*mut T` and `NonNull<T>`,
181 /// the offset is only well defined if we remain inside the same object or
182 /// one-past the end, and we can never land in a null pointer while obeying
183 /// those rules.
184 impl<T> PointerExtrust-analyzer cargo rawpointer 0.2.1 PointerExt# for NonNullrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/non_null/NonNull#<T> {
185 #[inline(always)]
186 unsafe fn offsetrust-analyzer cargo rawpointer 0.2.1 impl#[`NonNull<T>`][PointerExt]offset().(self, i: isize) -> Selfrust-analyzer cargo rawpointer 0.2.1 impl#[`NonNull<T>`][PointerExt] {
187 NonNullrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/non_null/NonNull#::new_uncheckedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/non_null/impl#[`NonNull<T>`]new_unchecked().(self.as_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/non_null/impl#[`NonNull<T>`]as_ptr().().offsetrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]offset().(i))
188 }
189 }
190
191
192 #[cfg(test)]
193 mod testsrust-analyzer cargo rawpointer 0.2.1 tests/ {
194 use superrust-analyzer cargo rawpointer 0.2.1 crate/::PointerExtrust-analyzer cargo rawpointer 0.2.1 PointerExt#;
195 use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/::NonNullrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/non_null/NonNull#;
196
197 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
198 fn it_worksrust-analyzer cargo rawpointer 0.2.1 tests/it_works().() {
199 unsafe {
200 let mut xs = [0; 16];
201 let mut ptr = xs.as_mut_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]as_mut_ptr().();
202 let end = ptr.offsetrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]offset().(4);
203 let mut i = 0;
204 while ptr rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().end {
205 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#deref().ptr.post_incrust-analyzer cargo rawpointer 0.2.1 PointerExt#post_inc().() = i;
206 i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`AddAssign<Self>`]add_assign().1;
207 }
208 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(&xs[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()...8]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()., &[0, 1, 2, 3, 0, 0, 0, 0]);
209 }
210 }
211
212 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
213 fn nonnullrust-analyzer cargo rawpointer 0.2.1 tests/nonnull().() {
214 unsafe {
215 let mut xs = [0; 16];
216 let mut ptr = NonNullrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/non_null/NonNull#::newrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/non_null/impl#[`NonNull<T>`]new().(xs.as_mut_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]as_mut_ptr().()).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().();
217 let end = ptr.offsetrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/non_null/impl#[`NonNull<T>`]offset().(4);
218 let mut i = 0;
219 while ptr rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().end {
220 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#deref().ptr.post_incrust-analyzer cargo rawpointer 0.2.1 PointerExt#post_inc().().as_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/non_null/impl#[`NonNull<T>`]as_ptr().() = i;
221 i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`AddAssign<Self>`]add_assign().1;
222 }
223 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(&xs[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()...8]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()., &[0, 1, 2, 3, 0, 0, 0, 0]);
224 }
225 }
226
227 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
228 fn nonnull_subrust-analyzer cargo rawpointer 0.2.1 tests/nonnull_sub().() {
229 unsafe {
230 // Test NonNull<T> .sub(1) iteration and equivalence to *mut T
231 let mut xs = [0; 16];
232 let mut ptr = xs.as_mut_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]as_mut_ptr().().addrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]add().(xs.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().());
233 let nptr = NonNullrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/non_null/NonNull#::newrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/non_null/impl#[`NonNull<T>`]new().(xs.as_mut_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]as_mut_ptr().()).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().();
234 let mut nend = nptr.addrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/non_null/impl#[`NonNull<T>`]add().(xs.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().());
235 let mut i = 0;
236 while nptr rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().nend {
237 nend = nend.subrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/non_null/impl#[`NonNull<T>`]sub().(1);
238 ptr = ptr.subrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]sub().(1);
239 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(nend.as_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/non_null/impl#[`NonNull<T>`]as_ptr().(), ptr);
240 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#deref().nend.as_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/non_null/impl#[`NonNull<T>`]as_ptr().() = i;
241 i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`AddAssign<Self>`]add_assign().1;
242 }
243 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(&xs[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()...8]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()., &[15, 14, 13, 12, 11, 10, 9, 8]);
244 }
245 }
246 }