Skip to content

Commit d0a237a

Browse files
committed
Update unstable methods and their doc tests
1 parent 4a8c18b commit d0a237a

File tree

1 file changed

+67
-52
lines changed

1 file changed

+67
-52
lines changed

src/ptr.rs

Lines changed: 67 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -332,9 +332,10 @@ where
332332
/// value
333333
/// })};
334334
/// ```
335-
pub unsafe fn map<F, U>(self, f: F) -> VolatilePtr<'a, U, ReadOnly>
335+
pub unsafe fn map<F, U>(self, f: F) -> VolatilePtr<'a, U, A::RestrictShared>
336336
where
337337
F: FnOnce(NonNull<T>) -> NonNull<U>,
338+
A: Access,
338339
U: ?Sized,
339340
{
340341
unsafe { VolatilePtr::new_restricted(Default::default(), f(self.pointer)) }
@@ -421,7 +422,7 @@ where
421422

422423
/// Methods for volatile slices
423424
#[cfg(feature = "unstable")]
424-
impl<'a, T, R, W> VolatilePtr<'a, [T], Access<R, W>> {
425+
impl<'a, T, A> VolatilePtr<'a, [T], A> {
425426
pub fn len(&self) -> usize {
426427
self.pointer.len()
427428
}
@@ -467,9 +468,10 @@ impl<'a, T, R, W> VolatilePtr<'a, [T], Access<R, W>> {
467468
pub fn index<I>(
468469
self,
469470
index: I,
470-
) -> VolatilePtr<'a, <I as SliceIndex<[T]>>::Output, Access<R, access_ptr::NoAccess>>
471+
) -> VolatilePtr<'a, <I as SliceIndex<[T]>>::Output, A::RestrictShared>
471472
where
472473
I: SliceIndex<[T]> + SliceIndex<[()]> + Clone,
474+
A: Access,
473475
{
474476
bounds_check(self.pointer.len(), index.clone());
475477

@@ -489,12 +491,10 @@ impl<'a, T, R, W> VolatilePtr<'a, [T], Access<R, W>> {
489491
}
490492
}
491493

492-
pub fn index_mut<I>(
493-
self,
494-
index: I,
495-
) -> VolatilePtr<'a, <I as SliceIndex<[T]>>::Output, Access<R, W>>
494+
pub fn index_mut<I>(self, index: I) -> VolatilePtr<'a, <I as SliceIndex<[T]>>::Output, A>
496495
where
497496
I: SliceIndex<[T]> + SliceIndex<[()]> + Clone,
497+
A: Access,
498498
{
499499
bounds_check(self.pointer.len(), index.clone());
500500

@@ -512,15 +512,18 @@ impl<'a, T, R, W> VolatilePtr<'a, [T], Access<R, W>> {
512512
}
513513

514514
/// Returns an iterator over the slice.
515-
pub fn iter(self) -> impl Iterator<Item = VolatilePtr<'a, T, Access<R, access_ptr::NoAccess>>> {
515+
pub fn iter(self) -> impl Iterator<Item = VolatilePtr<'a, T, A::RestrictShared>>
516+
where
517+
A: Access,
518+
{
516519
let ptr = self.as_ptr().as_ptr() as *mut T;
517520
let len = self.len();
518521
(0..len)
519522
.map(move |i| unsafe { VolatilePtr::new_generic(NonNull::new_unchecked(ptr.add(i))) })
520523
}
521524

522525
/// Returns an iterator that allows modifying each value.
523-
pub fn iter_mut(self) -> impl Iterator<Item = VolatilePtr<'a, T, Access<R, W>>> {
526+
pub fn iter_mut(self) -> impl Iterator<Item = VolatilePtr<'a, T, A>> {
524527
let ptr = self.as_ptr().as_ptr() as *mut T;
525528
let len = self.len();
526529
(0..len)
@@ -563,7 +566,7 @@ impl<'a, T, R, W> VolatilePtr<'a, [T], Access<R, W>> {
563566
pub fn copy_into_slice(&self, dst: &mut [T])
564567
where
565568
T: Copy,
566-
R: access_ptr::Safe,
569+
A: Readable,
567570
{
568571
let len = self.pointer.len();
569572
assert_eq!(
@@ -606,7 +609,7 @@ impl<'a, T, R, W> VolatilePtr<'a, [T], Access<R, W>> {
606609
/// let mut dst = [0, 0];
607610
/// // the `Volatile` type does not work with arrays, so convert `dst` to a slice
608611
/// let slice = &mut dst[..];
609-
/// let mut volatile = VolatilePtr::from_mut_ref(lice);
612+
/// let mut volatile = VolatilePtr::from_mut_ref(slice);
610613
/// /// // Because the slices have to be the same length,
611614
/// // we slice the source slice from four elements
612615
/// // to two. It will panic if we don't do this.
@@ -618,7 +621,7 @@ impl<'a, T, R, W> VolatilePtr<'a, [T], Access<R, W>> {
618621
pub fn copy_from_slice(&mut self, src: &[T])
619622
where
620623
T: Copy,
621-
W: access_ptr::Safe,
624+
A: Writable,
622625
{
623626
let len = self.pointer.len();
624627
assert_eq!(
@@ -664,15 +667,14 @@ impl<'a, T, R, W> VolatilePtr<'a, [T], Access<R, W>> {
664667
///
665668
/// let mut byte_array = *b"Hello, World!";
666669
/// let mut slice: &mut [u8] = &mut byte_array[..];
667-
/// let mut volatile = VolatilePtr::from_mut_ref(lice)) }; /
670+
/// let mut volatile = VolatilePtr::from_mut_ref(slice);
668671
/// volatile.copy_within(1..5, 8);
669672
///
670673
/// assert_eq!(&byte_array, b"Hello, Wello!");
671674
pub fn copy_within(&mut self, src: impl RangeBounds<usize>, dest: usize)
672675
where
673676
T: Copy,
674-
R: access_ptr::Safe,
675-
W: access_ptr::Safe,
677+
A: Readable + Writable,
676678
{
677679
let len = self.pointer.len();
678680
// implementation taken from https://github.com/rust-lang/rust/blob/683d1bcd405727fcc9209f64845bd3b9104878b8/library/core/src/slice/mod.rs#L2726-L2738
@@ -697,22 +699,22 @@ impl<'a, T, R, W> VolatilePtr<'a, [T], Access<R, W>> {
697699
self,
698700
mid: usize,
699701
) -> (
700-
VolatilePtr<'a, [T], Access<R, access_ptr::NoAccess>>,
701-
VolatilePtr<'a, [T], Access<R, access_ptr::NoAccess>>,
702-
) {
702+
VolatilePtr<'a, [T], A::RestrictShared>,
703+
VolatilePtr<'a, [T], A::RestrictShared>,
704+
)
705+
where
706+
A: Access,
707+
{
703708
assert!(mid <= self.pointer.len());
704709
// SAFETY: `[ptr; mid]` and `[mid; len]` are inside `self`, which
705710
// fulfills the requirements of `from_raw_parts_mut`.
706711
unsafe { self.split_at_unchecked(mid) }
707712
}
708713

709-
pub fn split_at_mut(
710-
self,
711-
mid: usize,
712-
) -> (
713-
VolatilePtr<'a, [T], Access<R, W>>,
714-
VolatilePtr<'a, [T], Access<R, W>>,
715-
) {
714+
pub fn split_at_mut(self, mid: usize) -> (VolatilePtr<'a, [T], A>, VolatilePtr<'a, [T], A>)
715+
where
716+
A: Access,
717+
{
716718
assert!(mid <= self.pointer.len());
717719
// SAFETY: `[ptr; mid]` and `[mid; len]` are inside `self`, which
718720
// fulfills the requirements of `from_raw_parts_mut`.
@@ -723,9 +725,12 @@ impl<'a, T, R, W> VolatilePtr<'a, [T], Access<R, W>> {
723725
self,
724726
mid: usize,
725727
) -> (
726-
VolatilePtr<'a, [T], Access<R, access_ptr::NoAccess>>,
727-
VolatilePtr<'a, [T], Access<R, access_ptr::NoAccess>>,
728-
) {
728+
VolatilePtr<'a, [T], A::RestrictShared>,
729+
VolatilePtr<'a, [T], A::RestrictShared>,
730+
)
731+
where
732+
A: Access,
733+
{
729734
// SAFETY: Caller has to check that `0 <= mid <= self.len()`
730735
unsafe {
731736
(
@@ -738,10 +743,10 @@ impl<'a, T, R, W> VolatilePtr<'a, [T], Access<R, W>> {
738743
unsafe fn split_at_mut_unchecked(
739744
self,
740745
mid: usize,
741-
) -> (
742-
VolatilePtr<'a, [T], Access<R, W>>,
743-
VolatilePtr<'a, [T], Access<R, W>>,
744-
) {
746+
) -> (VolatilePtr<'a, [T], A>, VolatilePtr<'a, [T], A>)
747+
where
748+
A: Access,
749+
{
745750
let len = self.pointer.len();
746751
let ptr = self.pointer.as_mut_ptr();
747752

@@ -764,9 +769,12 @@ impl<'a, T, R, W> VolatilePtr<'a, [T], Access<R, W>> {
764769
pub fn as_chunks<const N: usize>(
765770
self,
766771
) -> (
767-
VolatilePtr<'a, [[T; N]], Access<R, access_ptr::NoAccess>>,
768-
VolatilePtr<'a, [T], Access<R, access_ptr::NoAccess>>,
769-
) {
772+
VolatilePtr<'a, [[T; N]], <A::RestrictShared as Access>::RestrictShared>,
773+
VolatilePtr<'a, [T], A::RestrictShared>,
774+
)
775+
where
776+
A: Access,
777+
{
770778
assert_ne!(N, 0);
771779
let len = self.pointer.len() / N;
772780
let (multiple_of_n, remainder) = self.split_at(len * N);
@@ -778,7 +786,10 @@ impl<'a, T, R, W> VolatilePtr<'a, [T], Access<R, W>> {
778786

779787
pub unsafe fn as_chunks_unchecked<const N: usize>(
780788
self,
781-
) -> VolatilePtr<'a, [[T; N]], Access<R, access_ptr::NoAccess>> {
789+
) -> VolatilePtr<'a, [[T; N]], A::RestrictShared>
790+
where
791+
A: Access,
792+
{
782793
debug_assert_ne!(N, 0);
783794
debug_assert_eq!(self.pointer.len() % N, 0);
784795
let new_len =
@@ -796,10 +807,10 @@ impl<'a, T, R, W> VolatilePtr<'a, [T], Access<R, W>> {
796807

797808
pub fn as_chunks_mut<const N: usize>(
798809
self,
799-
) -> (
800-
VolatilePtr<'a, [[T; N]], Access<R, W>>,
801-
VolatilePtr<'a, [T], Access<R, W>>,
802-
) {
810+
) -> (VolatilePtr<'a, [[T; N]], A>, VolatilePtr<'a, [T], A>)
811+
where
812+
A: Access,
813+
{
803814
assert_ne!(N, 0);
804815
let len = self.pointer.len() / N;
805816
let (multiple_of_n, remainder) = self.split_at_mut(len * N);
@@ -809,9 +820,7 @@ impl<'a, T, R, W> VolatilePtr<'a, [T], Access<R, W>> {
809820
(array_slice, remainder)
810821
}
811822

812-
pub unsafe fn as_chunks_unchecked_mut<const N: usize>(
813-
self,
814-
) -> VolatilePtr<'a, [[T; N]], Access<R, W>> {
823+
pub unsafe fn as_chunks_unchecked_mut<const N: usize>(self) -> VolatilePtr<'a, [[T; N]], A> {
815824
debug_assert_ne!(N, 0);
816825
debug_assert_eq!(self.pointer.len() % N, 0);
817826
let new_len =
@@ -830,7 +839,7 @@ impl<'a, T, R, W> VolatilePtr<'a, [T], Access<R, W>> {
830839

831840
/// Methods for volatile byte slices
832841
#[cfg(feature = "unstable")]
833-
impl<R, W> VolatilePtr<'_, [u8], Access<R, W>> {
842+
impl<A> VolatilePtr<'_, [u8], A> {
834843
/// Sets all elements of the byte slice to the given `value` using a volatile `memset`.
835844
///
836845
/// This method is similar to the `slice::fill` method of the standard library, with the
@@ -848,13 +857,13 @@ impl<R, W> VolatilePtr<'_, [u8], Access<R, W>> {
848857
/// use core::ptr::NonNull;
849858
///
850859
/// let mut vec = vec![0; 10];
851-
/// let mut buf = VolatilePtr::from_mut_ref(ec.as_mut_slice());
860+
/// let mut buf = VolatilePtr::from_mut_ref(vec.as_mut_slice());
852861
/// buf.fill(1);
853862
/// assert_eq!(unsafe { buf.as_ptr().as_mut() }, &mut vec![1; 10]);
854863
/// ```
855864
pub fn fill(&mut self, value: u8)
856865
where
857-
W: access_ptr::Safe,
866+
A: Writable,
858867
{
859868
unsafe {
860869
intrinsics::volatile_set_memory(self.pointer.as_mut_ptr(), value, self.pointer.len());
@@ -867,7 +876,7 @@ impl<R, W> VolatilePtr<'_, [u8], Access<R, W>> {
867876
/// These methods are only available with the `unstable` feature enabled (requires a nightly
868877
/// Rust compiler).
869878
#[cfg(feature = "unstable")]
870-
impl<'a, T, R, W, const N: usize> VolatilePtr<'a, [T; N], Access<R, W>> {
879+
impl<'a, T, A, const N: usize> VolatilePtr<'a, [T; N], A> {
871880
/// Converts an array reference to a shared slice.
872881
///
873882
/// This makes it possible to use the methods defined on slices.
@@ -891,7 +900,10 @@ impl<'a, T, R, W, const N: usize> VolatilePtr<'a, [T; N], Access<R, W>> {
891900
///
892901
/// assert_eq!(dst, [1, 2]);
893902
/// ```
894-
pub fn as_slice(self) -> VolatilePtr<'a, [T], Access<R, access_ptr::NoAccess>> {
903+
pub fn as_slice(self) -> VolatilePtr<'a, [T], A::RestrictShared>
904+
where
905+
A: Access,
906+
{
895907
unsafe {
896908
self.map(|array| {
897909
NonNull::new(ptr::slice_from_raw_parts_mut(array.as_ptr() as *mut T, N)).unwrap()
@@ -908,12 +920,12 @@ impl<'a, T, R, W, const N: usize> VolatilePtr<'a, [T; N], Access<R, W>> {
908920
/// Copying two elements into a volatile array reference using `copy_from_slice`:
909921
///
910922
/// ```
911-
/// use volatile::VolatilePtr;
923+
/// use volatile::{access, VolatilePtr};
912924
/// use core::ptr::NonNull;
913925
///
914926
/// let src = [1, 2];
915927
/// let mut dst = [0, 0];
916-
/// let mut volatile = unsafe { VolatilePtr::new_write_only(NonNull::from(&dst)) };
928+
/// let mut volatile = unsafe { VolatilePtr::new_restricted(access::WriteOnly, NonNull::from(&dst)) };
917929
///
918930
/// // convert the `Volatile<[i32; 2]>` array reference to a `Volatile<[i32]>` slice
919931
/// let mut volatile_slice = volatile.as_slice_mut();
@@ -922,7 +934,10 @@ impl<'a, T, R, W, const N: usize> VolatilePtr<'a, [T; N], Access<R, W>> {
922934
///
923935
/// assert_eq!(dst, [1, 2]);
924936
/// ```
925-
pub fn as_slice_mut(self) -> VolatilePtr<'a, [T], Access<R, W>> {
937+
pub fn as_slice_mut(self) -> VolatilePtr<'a, [T], A>
938+
where
939+
A: Access,
940+
{
926941
unsafe {
927942
self.map_mut(|array| {
928943
NonNull::new(ptr::slice_from_raw_parts_mut(array.as_ptr() as *mut T, N)).unwrap()
@@ -946,5 +961,5 @@ fn bounds_check(len: usize, index: impl SliceIndex<[()]>) {
946961
const MAX_ARRAY: [(); usize::MAX] = [(); usize::MAX];
947962

948963
let bound_check_slice = &MAX_ARRAY[..len];
949-
&bound_check_slice[index];
964+
let _ = &bound_check_slice[index];
950965
}

0 commit comments

Comments
 (0)