Skip to content

Commit f21ef3f

Browse files
committed
Fix doctests
1 parent f3795cc commit f21ef3f

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

src/ptr.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use crate::access::{Access, ReadOnly, ReadWrite, Readable, Writable, WriteOnly};
3131
///
3232
/// struct Example { field_1: u32, field_2: u8, }
3333
/// let mut value = Example { field_1: 15, field_2: 255 };
34-
/// let mut volatile = unsafe { VolatilePtr::new_read_write(NonNull::from(&mut value)) };
34+
/// let mut volatile = VolatilePtr::from_mut_ref(&mut value);
3535
///
3636
/// // construct a volatile reference to a field
3737
/// let field_2 = map_field!(volatile.field_2);
@@ -46,7 +46,7 @@ use crate::access::{Access, ReadOnly, ReadWrite, Readable, Writable, WriteOnly};
4646
/// #[repr(packed)]
4747
/// struct Example { field_1: u8, field_2: usize, }
4848
/// let mut value = Example { field_1: 15, field_2: 255 };
49-
/// let mut volatile = unsafe { VolatilePtr::new_read_write(NonNull::from(&mut value)) };
49+
/// let mut volatile = VolatilePtr::from_mut_ref(&mut value);
5050
///
5151
/// // Constructing a volatile reference to an unaligned field doesn't compile.
5252
/// let field_2 = map_field!(volatile.field_2);
@@ -165,15 +165,17 @@ where
165165
/// ## Examples
166166
///
167167
/// ```rust
168-
/// use volatile::VolatilePtr;
168+
/// use volatile::{VolatilePtr, access};
169169
/// use core::ptr::NonNull;
170170
///
171171
/// let value = 42;
172-
/// let shared_reference = unsafe { VolatilePtr::new_read_only(NonNull::from(&value)) };
172+
/// let shared_reference = unsafe {
173+
/// VolatilePtr::new_restricted(access::ReadOnly, NonNull::from(&value))
174+
/// };
173175
/// assert_eq!(shared_reference.read(), 42);
174176
///
175177
/// let mut value = 50;
176-
/// let mut_reference = unsafe { VolatilePtr::new_read_write(NonNull::from(&mut value)) };
178+
/// let mut_reference = VolatilePtr::from_mut_ref(&mut value);
177179
/// assert_eq!(mut_reference.read(), 50);
178180
/// ```
179181
pub fn read(&self) -> T
@@ -198,7 +200,7 @@ where
198200
/// use core::ptr::NonNull;
199201
///
200202
/// let mut value = 42;
201-
/// let mut volatile = unsafe { VolatilePtr::new_read_write(NonNull::from(&mut value)) };
203+
/// let mut volatile = VolatilePtr::from_mut_ref(&mut value);
202204
/// volatile.write(50);
203205
///
204206
/// assert_eq!(volatile.read(), 50);
@@ -223,8 +225,8 @@ where
223225
/// use core::ptr::NonNull;
224226
///
225227
/// let mut value = 42;
226-
/// let mut volatile = unsafe { VolatilePtr::new_read_write(NonNull::from(&mut value)) };
227-
/// volatile.update(|val| *val += 1);
228+
/// let mut volatile = VolatilePtr::from_mut_ref(&mut value);
229+
/// volatile.update(|val| val + 1);
228230
///
229231
/// assert_eq!(volatile.read(), 43);
230232
/// ```
@@ -257,7 +259,7 @@ where
257259
/// use core::ptr::NonNull;
258260
///
259261
/// let mut value = 42;
260-
/// let mut volatile = unsafe { VolatilePtr::new_read_write(NonNull::from(&mut value)) };
262+
/// let mut volatile = VolatilePtr::from_mut_ref(&mut value);
261263
/// volatile.write(50);
262264
/// let unwrapped: *mut i32 = volatile.as_ptr().as_ptr();
263265
///
@@ -299,7 +301,7 @@ where
299301
///
300302
/// struct Example { field_1: u32, field_2: u8, }
301303
/// let mut value = Example { field_1: 15, field_2: 255 };
302-
/// let mut volatile = unsafe { VolatilePtr::new_read_write(NonNull::from(&mut value)) };
304+
/// let mut volatile = VolatilePtr::from_mut_ref(&mut value);
303305
///
304306
/// // construct a volatile reference to a field
305307
/// let field_2 = unsafe { volatile.map(|ptr| NonNull::new(core::ptr::addr_of_mut!((*ptr.as_ptr()).field_2)).unwrap()) };
@@ -313,7 +315,7 @@ where
313315
/// use core::ptr::NonNull;
314316
///
315317
/// let mut value = 5;
316-
/// let mut volatile = unsafe { VolatilePtr::new_read_write(NonNull::from(&mut value)) };
318+
/// let mut volatile = VolatilePtr::from_mut_ref(&mut value);
317319
///
318320
/// // DON'T DO THIS:
319321
/// let mut readout = 0;
@@ -375,7 +377,7 @@ where
375377
/// use core::ptr::NonNull;
376378
///
377379
/// let mut value: i16 = -4;
378-
/// let mut volatile = unsafe { VolatilePtr::new_read_write(NonNull::from(&mut value)) };
380+
/// let mut volatile = VolatilePtr::from_mut_ref(&mut value);
379381
///
380382
/// let read_only = volatile.read_only();
381383
/// assert_eq!(read_only.read(), -4);
@@ -397,7 +399,7 @@ where
397399
///
398400
/// struct Example { field_1: u32, field_2: u8, }
399401
/// let mut value = Example { field_1: 15, field_2: 255 };
400-
/// let mut volatile = unsafe { VolatilePtr::new_read_write(NonNull::from(&mut value)) };
402+
/// let mut volatile = VolatilePtr::from_mut_ref(&mut value);
401403
///
402404
/// // construct a volatile write-only reference to `field_2`
403405
/// let mut field_2 = map_field_mut!(volatile.field_2).write_only();
@@ -596,9 +598,8 @@ impl<'a, T, R, W> VolatilePtr<'a, [T], Access<R, W>> {
596598
/// let mut dst = [0, 0];
597599
/// // the `Volatile` type does not work with arrays, so convert `dst` to a slice
598600
/// let slice = &mut dst[..];
599-
/// let mut volatile = unsafe { VolatilePtr::new_read_write(NonNull::from(slice))};
600-
///
601-
/// // Because the slices have to be the same length,
601+
/// let mut volatile = VolatilePtr::from_mut_ref(lice);
602+
/// /// // Because the slices have to be the same length,
602603
/// // we slice the source slice from four elements
603604
/// // to two. It will panic if we don't do this.
604605
/// volatile.copy_from_slice(&src[2..]);
@@ -655,8 +656,7 @@ impl<'a, T, R, W> VolatilePtr<'a, [T], Access<R, W>> {
655656
///
656657
/// let mut byte_array = *b"Hello, World!";
657658
/// let mut slice: &mut [u8] = &mut byte_array[..];
658-
/// let mut volatile = unsafe { VolatilePtr::new_read_write(NonNull::from(slice)) };
659-
///
659+
/// let mut volatile = VolatilePtr::from_mut_ref(lice)) }; /
660660
/// volatile.copy_within(1..5, 8);
661661
///
662662
/// assert_eq!(&byte_array, b"Hello, Wello!");
@@ -840,7 +840,7 @@ impl<R, W> VolatilePtr<'_, [u8], Access<R, W>> {
840840
/// use core::ptr::NonNull;
841841
///
842842
/// let mut vec = vec![0; 10];
843-
/// let mut buf = unsafe { VolatilePtr::new_read_write(NonNull::from(vec.as_mut_slice())) };
843+
/// let mut buf = VolatilePtr::from_mut_ref(ec.as_mut_slice());
844844
/// buf.fill(1);
845845
/// assert_eq!(unsafe { buf.as_ptr().as_mut() }, &mut vec![1; 10]);
846846
/// ```

0 commit comments

Comments
 (0)