21
21
#![ warn( missing_docs) ]
22
22
#![ deny( unsafe_op_in_unsafe_fn) ]
23
23
24
- use access:: { Access , ReadOnly , ReadWrite , WriteOnly } ;
24
+ use access:: { Access , NoAccess , ReadOnly , ReadWrite , WriteOnly } ;
25
25
use core:: {
26
26
fmt,
27
27
marker:: PhantomData ,
@@ -337,10 +337,20 @@ where
337
337
}
338
338
339
339
/// Transformation methods for accessing struct fields
340
- impl < T , R , W > VolatilePtr < ' _ , T , Access < R , W > >
340
+ impl < ' a , T , R , W > VolatilePtr < ' a , T , Access < R , W > >
341
341
where
342
342
T : ?Sized ,
343
343
{
344
+ // TODO: Add documentation
345
+ pub fn borrow ( & self ) -> VolatilePtr < T , Access < R , NoAccess > > {
346
+ unsafe { VolatilePtr :: new_generic ( self . pointer ) }
347
+ }
348
+
349
+ // TODO: Add documentation
350
+ pub fn borrow_mut ( & mut self ) -> VolatilePtr < T , Access < R , W > > {
351
+ unsafe { VolatilePtr :: new_generic ( self . pointer ) }
352
+ }
353
+
344
354
/// Constructs a new `Volatile` reference by mapping the wrapped pointer.
345
355
///
346
356
/// This method is useful for accessing only a part of a volatile value, e.g. a subslice or
@@ -382,7 +392,7 @@ where
382
392
/// value
383
393
/// })};
384
394
/// ```
385
- pub unsafe fn map < ' a , F , U > ( & ' a self , f : F ) -> VolatilePtr < ' a , U , Access < R , access:: NoAccess > >
395
+ pub unsafe fn map < F , U > ( self , f : F ) -> VolatilePtr < ' a , U , Access < R , access:: NoAccess > >
386
396
where
387
397
F : FnOnce ( NonNull < T > ) -> NonNull < U > ,
388
398
U : ?Sized ,
@@ -391,8 +401,8 @@ where
391
401
}
392
402
393
403
#[ cfg( feature = "very_unstable" ) ]
394
- pub const unsafe fn map_const < ' a , F , U > (
395
- & ' a self ,
404
+ pub const unsafe fn map_const < F , U > (
405
+ self ,
396
406
f : F ,
397
407
) -> VolatilePtr < ' a , U , Access < R , access:: NoAccess > >
398
408
where
@@ -402,7 +412,7 @@ where
402
412
unsafe { VolatilePtr :: new_generic ( f ( self . pointer ) ) }
403
413
}
404
414
405
- pub unsafe fn map_mut < F , U > ( & mut self , f : F ) -> VolatilePtr < U , Access < R , W > >
415
+ pub unsafe fn map_mut < F , U > ( self , f : F ) -> VolatilePtr < ' a , U , Access < R , W > >
406
416
where
407
417
F : FnOnce ( NonNull < T > ) -> NonNull < U > ,
408
418
U : ?Sized ,
@@ -411,7 +421,7 @@ where
411
421
}
412
422
413
423
#[ cfg( feature = "very_unstable" ) ]
414
- pub const unsafe fn map_mut_const < F , U > ( & mut self , f : F ) -> VolatilePtr < U , Access < R , W > >
424
+ pub const unsafe fn map_mut_const < F , U > ( self , f : F ) -> VolatilePtr < ' a , U , Access < R , W > >
415
425
where
416
426
F : FnOnce ( NonNull < T > ) -> NonNull < U > ,
417
427
U : ?Sized ,
@@ -468,9 +478,9 @@ impl<'a, T, R, W> VolatilePtr<'a, [T], Access<R, W>> {
468
478
/// assert_eq!(subslice.index(0).read(), 2);
469
479
/// ```
470
480
pub fn index < I > (
471
- & self ,
481
+ self ,
472
482
index : I ,
473
- ) -> VolatilePtr < <I as SliceIndex < [ T ] > >:: Output , Access < R , access:: NoAccess > >
483
+ ) -> VolatilePtr < ' a , <I as SliceIndex < [ T ] > >:: Output , Access < R , access:: NoAccess > >
474
484
where
475
485
I : SliceIndex < [ T ] > + SliceIndex < [ ( ) ] > + Clone ,
476
486
{
@@ -480,7 +490,10 @@ impl<'a, T, R, W> VolatilePtr<'a, [T], Access<R, W>> {
480
490
}
481
491
482
492
#[ cfg( feature = "very_unstable" ) ]
483
- pub const fn index_const ( & self , index : usize ) -> VolatilePtr < T , Access < R , access:: NoAccess > > {
493
+ pub const fn index_const (
494
+ self ,
495
+ index : usize ,
496
+ ) -> VolatilePtr < ' a , T , Access < R , access:: NoAccess > > {
484
497
assert ! ( index < self . pointer. len( ) , "index out of bounds" ) ;
485
498
unsafe {
486
499
self . map_const ( |slice| {
@@ -490,9 +503,9 @@ impl<'a, T, R, W> VolatilePtr<'a, [T], Access<R, W>> {
490
503
}
491
504
492
505
pub fn index_mut < I > (
493
- & mut self ,
506
+ self ,
494
507
index : I ,
495
- ) -> VolatilePtr < <I as SliceIndex < [ T ] > >:: Output , Access < R , W > >
508
+ ) -> VolatilePtr < ' a , <I as SliceIndex < [ T ] > >:: Output , Access < R , W > >
496
509
where
497
510
I : SliceIndex < [ T ] > + SliceIndex < [ ( ) ] > + Clone ,
498
511
{
@@ -502,7 +515,7 @@ impl<'a, T, R, W> VolatilePtr<'a, [T], Access<R, W>> {
502
515
}
503
516
504
517
#[ cfg( feature = "very_unstable" ) ]
505
- pub const fn index_mut_const ( & mut self , index : usize ) -> VolatilePtr < T , Access < R , W > > {
518
+ pub const fn index_mut_const ( self , index : usize ) -> VolatilePtr < ' a , T , Access < R , W > > {
506
519
assert ! ( index < self . pointer. len( ) , "index out of bounds" ) ;
507
520
unsafe {
508
521
self . map_mut_const ( |slice| {
@@ -512,17 +525,15 @@ impl<'a, T, R, W> VolatilePtr<'a, [T], Access<R, W>> {
512
525
}
513
526
514
527
/// Returns an iterator over the slice.
515
- pub fn iter < ' b > (
516
- & ' b self ,
517
- ) -> impl Iterator < Item = VolatilePtr < ' b , T , Access < R , access:: NoAccess > > > + ' b {
528
+ pub fn iter ( self ) -> impl Iterator < Item = VolatilePtr < ' a , T , Access < R , access:: NoAccess > > > {
529
+ let ptr = self . as_ptr ( ) . as_ptr ( ) as * mut T ;
518
530
let len = self . len ( ) ;
519
- ( 0 ..len) . map ( move |i| self . index ( i) )
531
+ ( 0 ..len)
532
+ . map ( move |i| unsafe { VolatilePtr :: new_generic ( NonNull :: new_unchecked ( ptr. add ( i) ) ) } )
520
533
}
521
534
522
535
/// Returns an iterator that allows modifying each value.
523
- pub fn iter_mut < ' b > (
524
- & ' b mut self ,
525
- ) -> impl Iterator < Item = VolatilePtr < ' b , T , Access < R , W > > > + ' b {
536
+ pub fn iter_mut ( self ) -> impl Iterator < Item = VolatilePtr < ' a , T , Access < R , W > > > {
526
537
let ptr = self . as_ptr ( ) . as_ptr ( ) as * mut T ;
527
538
let len = self . len ( ) ;
528
539
( 0 ..len)
@@ -700,11 +711,11 @@ impl<'a, T, R, W> VolatilePtr<'a, [T], Access<R, W>> {
700
711
}
701
712
702
713
pub fn split_at (
703
- & self ,
714
+ self ,
704
715
mid : usize ,
705
716
) -> (
706
- VolatilePtr < [ T ] , Access < R , access:: NoAccess > > ,
707
- VolatilePtr < [ T ] , Access < R , access:: NoAccess > > ,
717
+ VolatilePtr < ' a , [ T ] , Access < R , access:: NoAccess > > ,
718
+ VolatilePtr < ' a , [ T ] , Access < R , access:: NoAccess > > ,
708
719
) {
709
720
assert ! ( mid <= self . pointer. len( ) ) ;
710
721
// SAFETY: `[ptr; mid]` and `[mid; len]` are inside `self`, which
@@ -713,11 +724,11 @@ impl<'a, T, R, W> VolatilePtr<'a, [T], Access<R, W>> {
713
724
}
714
725
715
726
pub fn split_at_mut (
716
- & mut self ,
727
+ self ,
717
728
mid : usize ,
718
729
) -> (
719
- VolatilePtr < [ T ] , Access < R , W > > ,
720
- VolatilePtr < [ T ] , Access < R , W > > ,
730
+ VolatilePtr < ' a , [ T ] , Access < R , W > > ,
731
+ VolatilePtr < ' a , [ T ] , Access < R , W > > ,
721
732
) {
722
733
assert ! ( mid <= self . pointer. len( ) ) ;
723
734
// SAFETY: `[ptr; mid]` and `[mid; len]` are inside `self`, which
@@ -726,11 +737,11 @@ impl<'a, T, R, W> VolatilePtr<'a, [T], Access<R, W>> {
726
737
}
727
738
728
739
unsafe fn split_at_unchecked (
729
- & self ,
740
+ self ,
730
741
mid : usize ,
731
742
) -> (
732
- VolatilePtr < [ T ] , Access < R , access:: NoAccess > > ,
733
- VolatilePtr < [ T ] , Access < R , access:: NoAccess > > ,
743
+ VolatilePtr < ' a , [ T ] , Access < R , access:: NoAccess > > ,
744
+ VolatilePtr < ' a , [ T ] , Access < R , access:: NoAccess > > ,
734
745
) {
735
746
// SAFETY: Caller has to check that `0 <= mid <= self.len()`
736
747
unsafe {
@@ -742,11 +753,11 @@ impl<'a, T, R, W> VolatilePtr<'a, [T], Access<R, W>> {
742
753
}
743
754
744
755
unsafe fn split_at_mut_unchecked (
745
- & mut self ,
756
+ self ,
746
757
mid : usize ,
747
758
) -> (
748
- VolatilePtr < [ T ] , Access < R , W > > ,
749
- VolatilePtr < [ T ] , Access < R , W > > ,
759
+ VolatilePtr < ' a , [ T ] , Access < R , W > > ,
760
+ VolatilePtr < ' a , [ T ] , Access < R , W > > ,
750
761
) {
751
762
let len = self . pointer . len ( ) ;
752
763
let ptr = self . pointer . as_mut_ptr ( ) ;
@@ -768,23 +779,23 @@ impl<'a, T, R, W> VolatilePtr<'a, [T], Access<R, W>> {
768
779
}
769
780
770
781
pub fn as_chunks < const N : usize > (
771
- & self ,
782
+ self ,
772
783
) -> (
773
- VolatilePtr < [ [ T ; N ] ] , Access < R , access:: NoAccess > > ,
774
- VolatilePtr < [ T ] , Access < R , access:: NoAccess > > ,
784
+ VolatilePtr < ' a , [ [ T ; N ] ] , Access < R , access:: NoAccess > > ,
785
+ VolatilePtr < ' a , [ T ] , Access < R , access:: NoAccess > > ,
775
786
) {
776
787
assert_ne ! ( N , 0 ) ;
777
788
let len = self . pointer . len ( ) / N ;
778
789
let ( multiple_of_n, remainder) = self . split_at ( len * N ) ;
779
790
// SAFETY: We already panicked for zero, and ensured by construction
780
791
// that the length of the subslice is a multiple of N.
781
- let array_slice = unsafe { multiple_of_n. as_chunks_unchecked_by_val ( ) } ;
792
+ let array_slice = unsafe { multiple_of_n. as_chunks_unchecked ( ) } ;
782
793
( array_slice, remainder)
783
794
}
784
795
785
796
pub unsafe fn as_chunks_unchecked < const N : usize > (
786
- & self ,
787
- ) -> VolatilePtr < [ [ T ; N ] ] , Access < R , access:: NoAccess > > {
797
+ self ,
798
+ ) -> VolatilePtr < ' a , [ [ T ; N ] ] , Access < R , access:: NoAccess > > {
788
799
debug_assert_ne ! ( N , 0 ) ;
789
800
debug_assert_eq ! ( self . pointer. len( ) % N , 0 ) ;
790
801
let new_len =
@@ -801,39 +812,21 @@ impl<'a, T, R, W> VolatilePtr<'a, [T], Access<R, W>> {
801
812
}
802
813
803
814
pub fn as_chunks_mut < const N : usize > (
804
- & mut self ,
815
+ self ,
805
816
) -> (
806
- VolatilePtr < [ [ T ; N ] ] , Access < R , W > > ,
807
- VolatilePtr < [ T ] , Access < R , W > > ,
817
+ VolatilePtr < ' a , [ [ T ; N ] ] , Access < R , W > > ,
818
+ VolatilePtr < ' a , [ T ] , Access < R , W > > ,
808
819
) {
809
820
assert_ne ! ( N , 0 ) ;
810
821
let len = self . pointer . len ( ) / N ;
811
822
let ( multiple_of_n, remainder) = self . split_at_mut ( len * N ) ;
812
823
// SAFETY: We already panicked for zero, and ensured by construction
813
824
// that the length of the subslice is a multiple of N.
814
- let array_slice = unsafe { multiple_of_n. as_chunks_unchecked_by_val ( ) } ;
825
+ let array_slice = unsafe { multiple_of_n. as_chunks_unchecked_mut ( ) } ;
815
826
( array_slice, remainder)
816
827
}
817
828
818
829
pub unsafe fn as_chunks_unchecked_mut < const N : usize > (
819
- & mut self ,
820
- ) -> VolatilePtr < [ [ T ; N ] ] , Access < R , W > > {
821
- debug_assert_ne ! ( N , 0 ) ;
822
- debug_assert_eq ! ( self . pointer. len( ) % N , 0 ) ;
823
- let new_len =
824
- // SAFETY: Our precondition is exactly what's needed to call this
825
- unsafe { core:: intrinsics:: exact_div ( self . pointer . len ( ) , N ) } ;
826
- // SAFETY: We cast a slice of `new_len * N` elements into
827
- // a slice of `new_len` many `N` elements chunks.
828
- let pointer = NonNull :: new ( ptr:: slice_from_raw_parts_mut (
829
- self . pointer . as_mut_ptr ( ) . cast ( ) ,
830
- new_len,
831
- ) )
832
- . unwrap ( ) ;
833
- unsafe { VolatilePtr :: new_generic ( pointer) }
834
- }
835
-
836
- pub unsafe fn as_chunks_unchecked_by_val < const N : usize > (
837
830
self ,
838
831
) -> VolatilePtr < ' a , [ [ T ; N ] ] , Access < R , W > > {
839
832
debug_assert_ne ! ( N , 0 ) ;
@@ -891,7 +884,7 @@ impl<R, W> VolatilePtr<'_, [u8], Access<R, W>> {
891
884
/// These methods are only available with the `unstable` feature enabled (requires a nightly
892
885
/// Rust compiler).
893
886
#[ cfg( feature = "unstable" ) ]
894
- impl < T , R , W , const N : usize > VolatilePtr < ' _ , [ T ; N ] , Access < R , W > > {
887
+ impl < ' a , T , R , W , const N : usize > VolatilePtr < ' a , [ T ; N ] , Access < R , W > > {
895
888
/// Converts an array reference to a shared slice.
896
889
///
897
890
/// This makes it possible to use the methods defined on slices.
@@ -916,7 +909,7 @@ impl<T, R, W, const N: usize> VolatilePtr<'_, [T; N], Access<R, W>> {
916
909
///
917
910
/// assert_eq!(dst, [1, 2]);
918
911
/// ```
919
- pub fn as_slice ( & self ) -> VolatilePtr < [ T ] , Access < R , access:: NoAccess > > {
912
+ pub fn as_slice ( self ) -> VolatilePtr < ' a , [ T ] , Access < R , access:: NoAccess > > {
920
913
unsafe {
921
914
self . map ( |array| {
922
915
NonNull :: new ( ptr:: slice_from_raw_parts_mut ( array. as_ptr ( ) as * mut T , N ) ) . unwrap ( )
@@ -948,7 +941,7 @@ impl<T, R, W, const N: usize> VolatilePtr<'_, [T; N], Access<R, W>> {
948
941
///
949
942
/// assert_eq!(dst, [1, 2]);
950
943
/// ```
951
- pub fn as_slice_mut < ' a > ( & ' a mut self ) -> VolatilePtr < ' a , [ T ] , Access < R , W > > {
944
+ pub fn as_slice_mut ( self ) -> VolatilePtr < ' a , [ T ] , Access < R , W > > {
952
945
unsafe {
953
946
self . map_mut ( |array| {
954
947
NonNull :: new ( ptr:: slice_from_raw_parts_mut ( array. as_ptr ( ) as * mut T , N ) ) . unwrap ( )
0 commit comments