@@ -332,9 +332,10 @@ where
332
332
/// value
333
333
/// })};
334
334
/// ```
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 >
336
336
where
337
337
F : FnOnce ( NonNull < T > ) -> NonNull < U > ,
338
+ A : Access ,
338
339
U : ?Sized ,
339
340
{
340
341
unsafe { VolatilePtr :: new_restricted ( Default :: default ( ) , f ( self . pointer ) ) }
@@ -421,7 +422,7 @@ where
421
422
422
423
/// Methods for volatile slices
423
424
#[ cfg( feature = "unstable" ) ]
424
- impl < ' a , T , R , W > VolatilePtr < ' a , [ T ] , Access < R , W > > {
425
+ impl < ' a , T , A > VolatilePtr < ' a , [ T ] , A > {
425
426
pub fn len ( & self ) -> usize {
426
427
self . pointer . len ( )
427
428
}
@@ -467,9 +468,10 @@ impl<'a, T, R, W> VolatilePtr<'a, [T], Access<R, W>> {
467
468
pub fn index < I > (
468
469
self ,
469
470
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 >
471
472
where
472
473
I : SliceIndex < [ T ] > + SliceIndex < [ ( ) ] > + Clone ,
474
+ A : Access ,
473
475
{
474
476
bounds_check ( self . pointer . len ( ) , index. clone ( ) ) ;
475
477
@@ -489,12 +491,10 @@ impl<'a, T, R, W> VolatilePtr<'a, [T], Access<R, W>> {
489
491
}
490
492
}
491
493
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 >
496
495
where
497
496
I : SliceIndex < [ T ] > + SliceIndex < [ ( ) ] > + Clone ,
497
+ A : Access ,
498
498
{
499
499
bounds_check ( self . pointer . len ( ) , index. clone ( ) ) ;
500
500
@@ -512,15 +512,18 @@ impl<'a, T, R, W> VolatilePtr<'a, [T], Access<R, W>> {
512
512
}
513
513
514
514
/// 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
+ {
516
519
let ptr = self . as_ptr ( ) . as_ptr ( ) as * mut T ;
517
520
let len = self . len ( ) ;
518
521
( 0 ..len)
519
522
. map ( move |i| unsafe { VolatilePtr :: new_generic ( NonNull :: new_unchecked ( ptr. add ( i) ) ) } )
520
523
}
521
524
522
525
/// 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 > > {
524
527
let ptr = self . as_ptr ( ) . as_ptr ( ) as * mut T ;
525
528
let len = self . len ( ) ;
526
529
( 0 ..len)
@@ -563,7 +566,7 @@ impl<'a, T, R, W> VolatilePtr<'a, [T], Access<R, W>> {
563
566
pub fn copy_into_slice ( & self , dst : & mut [ T ] )
564
567
where
565
568
T : Copy ,
566
- R : access_ptr :: Safe ,
569
+ A : Readable ,
567
570
{
568
571
let len = self . pointer . len ( ) ;
569
572
assert_eq ! (
@@ -606,7 +609,7 @@ impl<'a, T, R, W> VolatilePtr<'a, [T], Access<R, W>> {
606
609
/// let mut dst = [0, 0];
607
610
/// // the `Volatile` type does not work with arrays, so convert `dst` to a slice
608
611
/// let slice = &mut dst[..];
609
- /// let mut volatile = VolatilePtr::from_mut_ref(lice );
612
+ /// let mut volatile = VolatilePtr::from_mut_ref(slice );
610
613
/// /// // Because the slices have to be the same length,
611
614
/// // we slice the source slice from four elements
612
615
/// // 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>> {
618
621
pub fn copy_from_slice ( & mut self , src : & [ T ] )
619
622
where
620
623
T : Copy ,
621
- W : access_ptr :: Safe ,
624
+ A : Writable ,
622
625
{
623
626
let len = self . pointer . len ( ) ;
624
627
assert_eq ! (
@@ -664,15 +667,14 @@ impl<'a, T, R, W> VolatilePtr<'a, [T], Access<R, W>> {
664
667
///
665
668
/// let mut byte_array = *b"Hello, World!";
666
669
/// 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);
668
671
/// volatile.copy_within(1..5, 8);
669
672
///
670
673
/// assert_eq!(&byte_array, b"Hello, Wello!");
671
674
pub fn copy_within ( & mut self , src : impl RangeBounds < usize > , dest : usize )
672
675
where
673
676
T : Copy ,
674
- R : access_ptr:: Safe ,
675
- W : access_ptr:: Safe ,
677
+ A : Readable + Writable ,
676
678
{
677
679
let len = self . pointer . len ( ) ;
678
680
// 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>> {
697
699
self ,
698
700
mid : usize ,
699
701
) -> (
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
+ {
703
708
assert ! ( mid <= self . pointer. len( ) ) ;
704
709
// SAFETY: `[ptr; mid]` and `[mid; len]` are inside `self`, which
705
710
// fulfills the requirements of `from_raw_parts_mut`.
706
711
unsafe { self . split_at_unchecked ( mid) }
707
712
}
708
713
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
+ {
716
718
assert ! ( mid <= self . pointer. len( ) ) ;
717
719
// SAFETY: `[ptr; mid]` and `[mid; len]` are inside `self`, which
718
720
// fulfills the requirements of `from_raw_parts_mut`.
@@ -723,9 +725,12 @@ impl<'a, T, R, W> VolatilePtr<'a, [T], Access<R, W>> {
723
725
self ,
724
726
mid : usize ,
725
727
) -> (
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
+ {
729
734
// SAFETY: Caller has to check that `0 <= mid <= self.len()`
730
735
unsafe {
731
736
(
@@ -738,10 +743,10 @@ impl<'a, T, R, W> VolatilePtr<'a, [T], Access<R, W>> {
738
743
unsafe fn split_at_mut_unchecked (
739
744
self ,
740
745
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
+ {
745
750
let len = self . pointer . len ( ) ;
746
751
let ptr = self . pointer . as_mut_ptr ( ) ;
747
752
@@ -764,9 +769,12 @@ impl<'a, T, R, W> VolatilePtr<'a, [T], Access<R, W>> {
764
769
pub fn as_chunks < const N : usize > (
765
770
self ,
766
771
) -> (
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
+ {
770
778
assert_ne ! ( N , 0 ) ;
771
779
let len = self . pointer . len ( ) / N ;
772
780
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>> {
778
786
779
787
pub unsafe fn as_chunks_unchecked < const N : usize > (
780
788
self ,
781
- ) -> VolatilePtr < ' a , [ [ T ; N ] ] , Access < R , access_ptr:: NoAccess > > {
789
+ ) -> VolatilePtr < ' a , [ [ T ; N ] ] , A :: RestrictShared >
790
+ where
791
+ A : Access ,
792
+ {
782
793
debug_assert_ne ! ( N , 0 ) ;
783
794
debug_assert_eq ! ( self . pointer. len( ) % N , 0 ) ;
784
795
let new_len =
@@ -796,10 +807,10 @@ impl<'a, T, R, W> VolatilePtr<'a, [T], Access<R, W>> {
796
807
797
808
pub fn as_chunks_mut < const N : usize > (
798
809
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
+ {
803
814
assert_ne ! ( N , 0 ) ;
804
815
let len = self . pointer . len ( ) / N ;
805
816
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>> {
809
820
( array_slice, remainder)
810
821
}
811
822
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 > {
815
824
debug_assert_ne ! ( N , 0 ) ;
816
825
debug_assert_eq ! ( self . pointer. len( ) % N , 0 ) ;
817
826
let new_len =
@@ -830,7 +839,7 @@ impl<'a, T, R, W> VolatilePtr<'a, [T], Access<R, W>> {
830
839
831
840
/// Methods for volatile byte slices
832
841
#[ cfg( feature = "unstable" ) ]
833
- impl < R , W > VolatilePtr < ' _ , [ u8 ] , Access < R , W > > {
842
+ impl < A > VolatilePtr < ' _ , [ u8 ] , A > {
834
843
/// Sets all elements of the byte slice to the given `value` using a volatile `memset`.
835
844
///
836
845
/// 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>> {
848
857
/// use core::ptr::NonNull;
849
858
///
850
859
/// 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());
852
861
/// buf.fill(1);
853
862
/// assert_eq!(unsafe { buf.as_ptr().as_mut() }, &mut vec![1; 10]);
854
863
/// ```
855
864
pub fn fill ( & mut self , value : u8 )
856
865
where
857
- W : access_ptr :: Safe ,
866
+ A : Writable ,
858
867
{
859
868
unsafe {
860
869
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>> {
867
876
/// These methods are only available with the `unstable` feature enabled (requires a nightly
868
877
/// Rust compiler).
869
878
#[ 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 > {
871
880
/// Converts an array reference to a shared slice.
872
881
///
873
882
/// 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>> {
891
900
///
892
901
/// assert_eq!(dst, [1, 2]);
893
902
/// ```
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
+ {
895
907
unsafe {
896
908
self . map ( |array| {
897
909
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>> {
908
920
/// Copying two elements into a volatile array reference using `copy_from_slice`:
909
921
///
910
922
/// ```
911
- /// use volatile::VolatilePtr;
923
+ /// use volatile::{access, VolatilePtr} ;
912
924
/// use core::ptr::NonNull;
913
925
///
914
926
/// let src = [1, 2];
915
927
/// 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)) };
917
929
///
918
930
/// // convert the `Volatile<[i32; 2]>` array reference to a `Volatile<[i32]>` slice
919
931
/// 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>> {
922
934
///
923
935
/// assert_eq!(dst, [1, 2]);
924
936
/// ```
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
+ {
926
941
unsafe {
927
942
self . map_mut ( |array| {
928
943
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<[()]>) {
946
961
const MAX_ARRAY : [ ( ) ; usize:: MAX ] = [ ( ) ; usize:: MAX ] ;
947
962
948
963
let bound_check_slice = & MAX_ARRAY [ ..len] ;
949
- & bound_check_slice[ index] ;
964
+ let _ = & bound_check_slice[ index] ;
950
965
}
0 commit comments