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 ,
@@ -348,10 +348,20 @@ where
348
348
}
349
349
350
350
/// Transformation methods for accessing struct fields
351
- impl < T , R , W > VolatilePtr < ' _ , T , Access < R , W > >
351
+ impl < ' a , T , R , W > VolatilePtr < ' a , T , Access < R , W > >
352
352
where
353
353
T : ?Sized ,
354
354
{
355
+ // TODO: Add documentation
356
+ pub fn borrow ( & self ) -> VolatilePtr < T , Access < R , NoAccess > > {
357
+ unsafe { VolatilePtr :: new_generic ( self . pointer ) }
358
+ }
359
+
360
+ // TODO: Add documentation
361
+ pub fn borrow_mut ( & mut self ) -> VolatilePtr < T , Access < R , W > > {
362
+ unsafe { VolatilePtr :: new_generic ( self . pointer ) }
363
+ }
364
+
355
365
/// Constructs a new `Volatile` reference by mapping the wrapped pointer.
356
366
///
357
367
/// This method is useful for accessing only a part of a volatile value, e.g. a subslice or
@@ -394,7 +404,7 @@ where
394
404
/// })};
395
405
/// ```
396
406
#[ inline]
397
- pub unsafe fn map < ' a , F , U > ( & ' a self , f : F ) -> VolatilePtr < ' a , U , Access < R , access:: NoAccess > >
407
+ pub unsafe fn map < F , U > ( self , f : F ) -> VolatilePtr < ' a , U , Access < R , access:: NoAccess > >
398
408
where
399
409
F : FnOnce ( NonNull < T > ) -> NonNull < U > ,
400
410
U : ?Sized ,
@@ -404,8 +414,8 @@ where
404
414
405
415
#[ cfg( feature = "very_unstable" ) ]
406
416
#[ inline]
407
- pub const unsafe fn map_const < ' a , F , U > (
408
- & ' a self ,
417
+ pub const unsafe fn map_const < F , U > (
418
+ self ,
409
419
f : F ,
410
420
) -> VolatilePtr < ' a , U , Access < R , access:: NoAccess > >
411
421
where
@@ -416,7 +426,7 @@ where
416
426
}
417
427
418
428
#[ inline]
419
- pub unsafe fn map_mut < F , U > ( & mut self , f : F ) -> VolatilePtr < U , Access < R , W > >
429
+ pub unsafe fn map_mut < F , U > ( self , f : F ) -> VolatilePtr < ' a , U , Access < R , W > >
420
430
where
421
431
F : FnOnce ( NonNull < T > ) -> NonNull < U > ,
422
432
U : ?Sized ,
@@ -426,7 +436,7 @@ where
426
436
427
437
#[ cfg( feature = "very_unstable" ) ]
428
438
#[ inline]
429
- pub const unsafe fn map_mut_const < F , U > ( & mut self , f : F ) -> VolatilePtr < U , Access < R , W > >
439
+ pub const unsafe fn map_mut_const < F , U > ( self , f : F ) -> VolatilePtr < ' a , U , Access < R , W > >
430
440
where
431
441
F : FnOnce ( NonNull < T > ) -> NonNull < U > ,
432
442
U : ?Sized ,
@@ -486,9 +496,9 @@ impl<'a, T, R, W> VolatilePtr<'a, [T], Access<R, W>> {
486
496
/// ```
487
497
#[ inline]
488
498
pub fn index < I > (
489
- & self ,
499
+ self ,
490
500
index : I ,
491
- ) -> VolatilePtr < <I as SliceIndex < [ T ] > >:: Output , Access < R , access:: NoAccess > >
501
+ ) -> VolatilePtr < ' a , <I as SliceIndex < [ T ] > >:: Output , Access < R , access:: NoAccess > >
492
502
where
493
503
I : SliceIndex < [ T ] > + SliceIndex < [ ( ) ] > + Clone ,
494
504
{
@@ -499,7 +509,10 @@ impl<'a, T, R, W> VolatilePtr<'a, [T], Access<R, W>> {
499
509
500
510
#[ cfg( feature = "very_unstable" ) ]
501
511
#[ inline]
502
- pub const fn index_const ( & self , index : usize ) -> VolatilePtr < T , Access < R , access:: NoAccess > > {
512
+ pub const fn index_const (
513
+ self ,
514
+ index : usize ,
515
+ ) -> VolatilePtr < ' a , T , Access < R , access:: NoAccess > > {
503
516
assert ! ( index < self . pointer. len( ) , "index out of bounds" ) ;
504
517
unsafe {
505
518
self . map_const ( |slice| {
@@ -510,9 +523,9 @@ impl<'a, T, R, W> VolatilePtr<'a, [T], Access<R, W>> {
510
523
511
524
#[ inline]
512
525
pub fn index_mut < I > (
513
- & mut self ,
526
+ self ,
514
527
index : I ,
515
- ) -> VolatilePtr < <I as SliceIndex < [ T ] > >:: Output , Access < R , W > >
528
+ ) -> VolatilePtr < ' a , <I as SliceIndex < [ T ] > >:: Output , Access < R , W > >
516
529
where
517
530
I : SliceIndex < [ T ] > + SliceIndex < [ ( ) ] > + Clone ,
518
531
{
@@ -523,7 +536,7 @@ impl<'a, T, R, W> VolatilePtr<'a, [T], Access<R, W>> {
523
536
524
537
#[ cfg( feature = "very_unstable" ) ]
525
538
#[ inline]
526
- pub const fn index_mut_const ( & mut self , index : usize ) -> VolatilePtr < T , Access < R , W > > {
539
+ pub const fn index_mut_const ( self , index : usize ) -> VolatilePtr < ' a , T , Access < R , W > > {
527
540
assert ! ( index < self . pointer. len( ) , "index out of bounds" ) ;
528
541
unsafe {
529
542
self . map_mut_const ( |slice| {
@@ -534,18 +547,16 @@ impl<'a, T, R, W> VolatilePtr<'a, [T], Access<R, W>> {
534
547
535
548
/// Returns an iterator over the slice.
536
549
#[ inline]
537
- pub fn iter < ' b > (
538
- & ' b self ,
539
- ) -> impl Iterator < Item = VolatilePtr < ' b , T , Access < R , access:: NoAccess > > > + ' b {
550
+ pub fn iter ( self ) -> impl Iterator < Item = VolatilePtr < ' a , T , Access < R , access:: NoAccess > > > {
551
+ let ptr = self . as_ptr ( ) . as_ptr ( ) as * mut T ;
540
552
let len = self . len ( ) ;
541
- ( 0 ..len) . map ( move |i| self . index ( i) )
553
+ ( 0 ..len)
554
+ . map ( move |i| unsafe { VolatilePtr :: new_generic ( NonNull :: new_unchecked ( ptr. add ( i) ) ) } )
542
555
}
543
556
544
557
/// Returns an iterator that allows modifying each value.
545
558
#[ inline]
546
- pub fn iter_mut < ' b > (
547
- & ' b mut self ,
548
- ) -> impl Iterator < Item = VolatilePtr < ' b , T , Access < R , W > > > + ' b {
559
+ pub fn iter_mut ( self ) -> impl Iterator < Item = VolatilePtr < ' a , T , Access < R , W > > > {
549
560
let ptr = self . as_ptr ( ) . as_ptr ( ) as * mut T ;
550
561
let len = self . len ( ) ;
551
562
( 0 ..len)
@@ -727,11 +738,11 @@ impl<'a, T, R, W> VolatilePtr<'a, [T], Access<R, W>> {
727
738
728
739
#[ inline]
729
740
pub fn split_at (
730
- & self ,
741
+ self ,
731
742
mid : usize ,
732
743
) -> (
733
- VolatilePtr < [ T ] , Access < R , access:: NoAccess > > ,
734
- VolatilePtr < [ T ] , Access < R , access:: NoAccess > > ,
744
+ VolatilePtr < ' a , [ T ] , Access < R , access:: NoAccess > > ,
745
+ VolatilePtr < ' a , [ T ] , Access < R , access:: NoAccess > > ,
735
746
) {
736
747
assert ! ( mid <= self . pointer. len( ) ) ;
737
748
// SAFETY: `[ptr; mid]` and `[mid; len]` are inside `self`, which
@@ -741,11 +752,11 @@ impl<'a, T, R, W> VolatilePtr<'a, [T], Access<R, W>> {
741
752
742
753
#[ inline]
743
754
pub fn split_at_mut (
744
- & mut self ,
755
+ self ,
745
756
mid : usize ,
746
757
) -> (
747
- VolatilePtr < [ T ] , Access < R , W > > ,
748
- VolatilePtr < [ T ] , Access < R , W > > ,
758
+ VolatilePtr < ' a , [ T ] , Access < R , W > > ,
759
+ VolatilePtr < ' a , [ T ] , Access < R , W > > ,
749
760
) {
750
761
assert ! ( mid <= self . pointer. len( ) ) ;
751
762
// SAFETY: `[ptr; mid]` and `[mid; len]` are inside `self`, which
@@ -755,11 +766,11 @@ impl<'a, T, R, W> VolatilePtr<'a, [T], Access<R, W>> {
755
766
756
767
#[ inline]
757
768
unsafe fn split_at_unchecked (
758
- & self ,
769
+ self ,
759
770
mid : usize ,
760
771
) -> (
761
- VolatilePtr < [ T ] , Access < R , access:: NoAccess > > ,
762
- VolatilePtr < [ T ] , Access < R , access:: NoAccess > > ,
772
+ VolatilePtr < ' a , [ T ] , Access < R , access:: NoAccess > > ,
773
+ VolatilePtr < ' a , [ T ] , Access < R , access:: NoAccess > > ,
763
774
) {
764
775
// SAFETY: Caller has to check that `0 <= mid <= self.len()`
765
776
unsafe {
@@ -772,11 +783,11 @@ impl<'a, T, R, W> VolatilePtr<'a, [T], Access<R, W>> {
772
783
773
784
#[ inline]
774
785
unsafe fn split_at_mut_unchecked (
775
- & mut self ,
786
+ self ,
776
787
mid : usize ,
777
788
) -> (
778
- VolatilePtr < [ T ] , Access < R , W > > ,
779
- VolatilePtr < [ T ] , Access < R , W > > ,
789
+ VolatilePtr < ' a , [ T ] , Access < R , W > > ,
790
+ VolatilePtr < ' a , [ T ] , Access < R , W > > ,
780
791
) {
781
792
let len = self . pointer . len ( ) ;
782
793
let ptr = self . pointer . as_mut_ptr ( ) ;
@@ -799,24 +810,24 @@ impl<'a, T, R, W> VolatilePtr<'a, [T], Access<R, W>> {
799
810
800
811
#[ inline]
801
812
pub fn as_chunks < const N : usize > (
802
- & self ,
813
+ self ,
803
814
) -> (
804
- VolatilePtr < [ [ T ; N ] ] , Access < R , access:: NoAccess > > ,
805
- VolatilePtr < [ T ] , Access < R , access:: NoAccess > > ,
815
+ VolatilePtr < ' a , [ [ T ; N ] ] , Access < R , access:: NoAccess > > ,
816
+ VolatilePtr < ' a , [ T ] , Access < R , access:: NoAccess > > ,
806
817
) {
807
818
assert_ne ! ( N , 0 ) ;
808
819
let len = self . pointer . len ( ) / N ;
809
820
let ( multiple_of_n, remainder) = self . split_at ( len * N ) ;
810
821
// SAFETY: We already panicked for zero, and ensured by construction
811
822
// that the length of the subslice is a multiple of N.
812
- let array_slice = unsafe { multiple_of_n. as_chunks_unchecked_by_val ( ) } ;
823
+ let array_slice = unsafe { multiple_of_n. as_chunks_unchecked ( ) } ;
813
824
( array_slice, remainder)
814
825
}
815
826
816
827
#[ inline]
817
828
pub unsafe fn as_chunks_unchecked < const N : usize > (
818
- & self ,
819
- ) -> VolatilePtr < [ [ T ; N ] ] , Access < R , access:: NoAccess > > {
829
+ self ,
830
+ ) -> VolatilePtr < ' a , [ [ T ; N ] ] , Access < R , access:: NoAccess > > {
820
831
debug_assert_ne ! ( N , 0 ) ;
821
832
debug_assert_eq ! ( self . pointer. len( ) % N , 0 ) ;
822
833
let new_len =
@@ -834,41 +845,22 @@ impl<'a, T, R, W> VolatilePtr<'a, [T], Access<R, W>> {
834
845
835
846
#[ inline]
836
847
pub fn as_chunks_mut < const N : usize > (
837
- & mut self ,
848
+ self ,
838
849
) -> (
839
- VolatilePtr < [ [ T ; N ] ] , Access < R , W > > ,
840
- VolatilePtr < [ T ] , Access < R , W > > ,
850
+ VolatilePtr < ' a , [ [ T ; N ] ] , Access < R , W > > ,
851
+ VolatilePtr < ' a , [ T ] , Access < R , W > > ,
841
852
) {
842
853
assert_ne ! ( N , 0 ) ;
843
854
let len = self . pointer . len ( ) / N ;
844
855
let ( multiple_of_n, remainder) = self . split_at_mut ( len * N ) ;
845
856
// SAFETY: We already panicked for zero, and ensured by construction
846
857
// that the length of the subslice is a multiple of N.
847
- let array_slice = unsafe { multiple_of_n. as_chunks_unchecked_by_val ( ) } ;
858
+ let array_slice = unsafe { multiple_of_n. as_chunks_unchecked_mut ( ) } ;
848
859
( array_slice, remainder)
849
860
}
850
861
851
862
#[ inline]
852
863
pub unsafe fn as_chunks_unchecked_mut < const N : usize > (
853
- & mut self ,
854
- ) -> VolatilePtr < [ [ T ; N ] ] , Access < R , W > > {
855
- debug_assert_ne ! ( N , 0 ) ;
856
- debug_assert_eq ! ( self . pointer. len( ) % N , 0 ) ;
857
- let new_len =
858
- // SAFETY: Our precondition is exactly what's needed to call this
859
- unsafe { core:: intrinsics:: exact_div ( self . pointer . len ( ) , N ) } ;
860
- // SAFETY: We cast a slice of `new_len * N` elements into
861
- // a slice of `new_len` many `N` elements chunks.
862
- let pointer = NonNull :: new ( ptr:: slice_from_raw_parts_mut (
863
- self . pointer . as_mut_ptr ( ) . cast ( ) ,
864
- new_len,
865
- ) )
866
- . unwrap ( ) ;
867
- unsafe { VolatilePtr :: new_generic ( pointer) }
868
- }
869
-
870
- #[ inline]
871
- pub unsafe fn as_chunks_unchecked_by_val < const N : usize > (
872
864
self ,
873
865
) -> VolatilePtr < ' a , [ [ T ; N ] ] , Access < R , W > > {
874
866
debug_assert_ne ! ( N , 0 ) ;
@@ -927,7 +919,7 @@ impl<R, W> VolatilePtr<'_, [u8], Access<R, W>> {
927
919
/// These methods are only available with the `unstable` feature enabled (requires a nightly
928
920
/// Rust compiler).
929
921
#[ cfg( feature = "unstable" ) ]
930
- impl < T , R , W , const N : usize > VolatilePtr < ' _ , [ T ; N ] , Access < R , W > > {
922
+ impl < ' a , T , R , W , const N : usize > VolatilePtr < ' a , [ T ; N ] , Access < R , W > > {
931
923
/// Converts an array reference to a shared slice.
932
924
///
933
925
/// This makes it possible to use the methods defined on slices.
@@ -953,7 +945,7 @@ impl<T, R, W, const N: usize> VolatilePtr<'_, [T; N], Access<R, W>> {
953
945
/// assert_eq!(dst, [1, 2]);
954
946
/// ```
955
947
#[ inline]
956
- pub fn as_slice ( & self ) -> VolatilePtr < [ T ] , Access < R , access:: NoAccess > > {
948
+ pub fn as_slice ( self ) -> VolatilePtr < ' a , [ T ] , Access < R , access:: NoAccess > > {
957
949
unsafe {
958
950
self . map ( |array| {
959
951
NonNull :: new ( ptr:: slice_from_raw_parts_mut ( array. as_ptr ( ) as * mut T , N ) ) . unwrap ( )
@@ -986,7 +978,7 @@ impl<T, R, W, const N: usize> VolatilePtr<'_, [T; N], Access<R, W>> {
986
978
/// assert_eq!(dst, [1, 2]);
987
979
/// ```
988
980
#[ inline]
989
- pub fn as_slice_mut < ' a > ( & ' a mut self ) -> VolatilePtr < ' a , [ T ] , Access < R , W > > {
981
+ pub fn as_slice_mut ( self ) -> VolatilePtr < ' a , [ T ] , Access < R , W > > {
990
982
unsafe {
991
983
self . map_mut ( |array| {
992
984
NonNull :: new ( ptr:: slice_from_raw_parts_mut ( array. as_ptr ( ) as * mut T , N ) ) . unwrap ( )
0 commit comments