@@ -10,7 +10,7 @@ use core::{
10
10
ops:: { Deref , DerefMut } ,
11
11
ptr:: NonNull ,
12
12
result:: Result as CoreResult ,
13
- slice:: from_raw_parts_mut,
13
+ slice:: { from_raw_parts , from_raw_parts_mut} ,
14
14
sync:: atomic:: {
15
15
AtomicBool , AtomicUsize ,
16
16
Ordering :: { AcqRel , Acquire , Release } ,
@@ -404,9 +404,10 @@ impl<'a, const N: usize> Producer<'a, N> {
404
404
let grant_slice = unsafe { from_raw_parts_mut ( start_of_buf_ptr. add ( start) , sz) } ;
405
405
406
406
Ok ( GrantW {
407
- buf : grant_slice,
407
+ buf : grant_slice. into ( ) ,
408
408
bbq : self . bbq ,
409
409
to_commit : 0 ,
410
+ phatom : PhantomData ,
410
411
} )
411
412
}
412
413
@@ -507,9 +508,10 @@ impl<'a, const N: usize> Producer<'a, N> {
507
508
let grant_slice = unsafe { from_raw_parts_mut ( start_of_buf_ptr. add ( start) , sz) } ;
508
509
509
510
Ok ( GrantW {
510
- buf : grant_slice,
511
+ buf : grant_slice. into ( ) ,
511
512
bbq : self . bbq ,
512
513
to_commit : 0 ,
514
+ phatom : PhantomData ,
513
515
} )
514
516
}
515
517
}
@@ -598,9 +600,10 @@ impl<'a, const N: usize> Consumer<'a, N> {
598
600
let grant_slice = unsafe { from_raw_parts_mut ( start_of_buf_ptr. add ( read) , sz) } ;
599
601
600
602
Ok ( GrantR {
601
- buf : grant_slice,
603
+ buf : grant_slice. into ( ) ,
602
604
bbq : self . bbq ,
603
605
to_release : 0 ,
606
+ phatom : PhantomData ,
604
607
} )
605
608
}
606
609
@@ -651,10 +654,11 @@ impl<'a, const N: usize> Consumer<'a, N> {
651
654
let grant_slice2 = unsafe { from_raw_parts_mut ( start_of_buf_ptr, sz2) } ;
652
655
653
656
Ok ( SplitGrantR {
654
- buf1 : grant_slice1,
655
- buf2 : grant_slice2,
657
+ buf1 : grant_slice1. into ( ) ,
658
+ buf2 : grant_slice2. into ( ) ,
656
659
bbq : self . bbq ,
657
660
to_release : 0 ,
661
+ phatom : PhantomData ,
658
662
} )
659
663
}
660
664
}
@@ -697,9 +701,10 @@ impl<const N: usize> BBBuffer<N> {
697
701
/// without committing it takes a short critical section,
698
702
#[ derive( Debug , PartialEq ) ]
699
703
pub struct GrantW < ' a , const N : usize > {
700
- pub ( crate ) buf : & ' a mut [ u8 ] ,
704
+ pub ( crate ) buf : NonNull < [ u8 ] > ,
701
705
bbq : NonNull < BBBuffer < N > > ,
702
706
pub ( crate ) to_commit : usize ,
707
+ phatom : PhantomData < & ' a mut [ u8 ] > ,
703
708
}
704
709
705
710
unsafe impl < ' a , const N : usize > Send for GrantW < ' a , N > { }
@@ -718,20 +723,22 @@ unsafe impl<'a, const N: usize> Send for GrantW<'a, N> {}
718
723
/// without releasing it takes a short critical section,
719
724
#[ derive( Debug , PartialEq ) ]
720
725
pub struct GrantR < ' a , const N : usize > {
721
- pub ( crate ) buf : & ' a mut [ u8 ] ,
726
+ pub ( crate ) buf : NonNull < [ u8 ] > ,
722
727
bbq : NonNull < BBBuffer < N > > ,
723
728
pub ( crate ) to_release : usize ,
729
+ phatom : PhantomData < & ' a mut [ u8 ] > ,
724
730
}
725
731
726
732
/// A structure representing up to two contiguous regions of memory that
727
733
/// may be read from, and potentially "released" (or cleared)
728
734
/// from the queue
729
735
#[ derive( Debug , PartialEq ) ]
730
736
pub struct SplitGrantR < ' a , const N : usize > {
731
- pub ( crate ) buf1 : & ' a mut [ u8 ] ,
732
- pub ( crate ) buf2 : & ' a mut [ u8 ] ,
737
+ pub ( crate ) buf1 : NonNull < [ u8 ] > ,
738
+ pub ( crate ) buf2 : NonNull < [ u8 ] > ,
733
739
bbq : NonNull < BBBuffer < N > > ,
734
740
pub ( crate ) to_release : usize ,
741
+ phatom : PhantomData < & ' a mut [ u8 ] > ,
735
742
}
736
743
737
744
unsafe impl < ' a , const N : usize > Send for GrantR < ' a , N > { }
@@ -777,7 +784,7 @@ impl<'a, const N: usize> GrantW<'a, N> {
777
784
/// # }
778
785
/// ```
779
786
pub fn buf ( & mut self ) -> & mut [ u8 ] {
780
- self . buf
787
+ unsafe { from_raw_parts_mut ( self . buf . as_ptr ( ) as * mut u8 , self . buf . len ( ) ) }
781
788
}
782
789
783
790
/// Sometimes, it's not possible for the lifetimes to check out. For example,
@@ -794,7 +801,7 @@ impl<'a, const N: usize> GrantW<'a, N> {
794
801
/// Additionally, you must ensure that a separate reference to this data is not created
795
802
/// to this data, e.g. using `DerefMut` or the `buf()` method of this grant.
796
803
pub unsafe fn as_static_mut_buf ( & mut self ) -> & ' static mut [ u8 ] {
797
- transmute :: < & mut [ u8 ] , & ' static mut [ u8 ] > ( self . buf )
804
+ transmute :: < & mut [ u8 ] , & ' static mut [ u8 ] > ( self . buf ( ) )
798
805
}
799
806
800
807
#[ inline( always) ]
@@ -875,9 +882,9 @@ impl<'a, const N: usize> GrantR<'a, N> {
875
882
876
883
pub ( crate ) fn shrink ( & mut self , len : usize ) {
877
884
let mut new_buf: & mut [ u8 ] = & mut [ ] ;
878
- core:: mem:: swap ( & mut self . buf , & mut new_buf) ;
885
+ core:: mem:: swap ( & mut self . buf_mut ( ) , & mut new_buf) ;
879
886
let ( new, _) = new_buf. split_at_mut ( len) ;
880
- self . buf = new;
887
+ self . buf = new. into ( ) ;
881
888
}
882
889
883
890
/// Obtain access to the inner buffer for reading
@@ -910,15 +917,15 @@ impl<'a, const N: usize> GrantR<'a, N> {
910
917
/// # }
911
918
/// ```
912
919
pub fn buf ( & self ) -> & [ u8 ] {
913
- self . buf
920
+ unsafe { from_raw_parts ( self . buf . as_ptr ( ) as * const u8 , self . buf . len ( ) ) }
914
921
}
915
922
916
923
/// Obtain mutable access to the read grant
917
924
///
918
925
/// This is useful if you are performing in-place operations
919
926
/// on an incoming packet, such as decryption
920
927
pub fn buf_mut ( & mut self ) -> & mut [ u8 ] {
921
- self . buf
928
+ unsafe { from_raw_parts_mut ( self . buf . as_ptr ( ) as * mut u8 , self . buf . len ( ) ) }
922
929
}
923
930
924
931
/// Sometimes, it's not possible for the lifetimes to check out. For example,
@@ -935,7 +942,7 @@ impl<'a, const N: usize> GrantR<'a, N> {
935
942
/// Additionally, you must ensure that a separate reference to this data is not created
936
943
/// to this data, e.g. using `Deref` or the `buf()` method of this grant.
937
944
pub unsafe fn as_static_buf ( & self ) -> & ' static [ u8 ] {
938
- transmute :: < & [ u8 ] , & ' static [ u8 ] > ( self . buf )
945
+ transmute :: < & [ u8 ] , & ' static [ u8 ] > ( self . buf ( ) )
939
946
}
940
947
941
948
#[ inline( always) ]
@@ -1022,15 +1029,19 @@ impl<'a, const N: usize> SplitGrantR<'a, N> {
1022
1029
/// # }
1023
1030
/// ```
1024
1031
pub fn bufs ( & self ) -> ( & [ u8 ] , & [ u8 ] ) {
1025
- ( self . buf1 , self . buf2 )
1032
+ let buf1 = unsafe { from_raw_parts ( self . buf1 . as_ptr ( ) as * const u8 , self . buf1 . len ( ) ) } ;
1033
+ let buf2 = unsafe { from_raw_parts ( self . buf2 . as_ptr ( ) as * const u8 , self . buf2 . len ( ) ) } ;
1034
+ ( buf1, buf2)
1026
1035
}
1027
1036
1028
1037
/// Obtain mutable access to both parts of the read grant
1029
1038
///
1030
1039
/// This is useful if you are performing in-place operations
1031
1040
/// on an incoming packet, such as decryption
1032
1041
pub fn bufs_mut ( & mut self ) -> ( & mut [ u8 ] , & mut [ u8 ] ) {
1033
- ( self . buf1 , self . buf2 )
1042
+ let buf1 = unsafe { from_raw_parts_mut ( self . buf1 . as_ptr ( ) as * mut u8 , self . buf1 . len ( ) ) } ;
1043
+ let buf2 = unsafe { from_raw_parts_mut ( self . buf2 . as_ptr ( ) as * mut u8 , self . buf2 . len ( ) ) } ;
1044
+ ( buf1, buf2)
1034
1045
}
1035
1046
1036
1047
#[ inline( always) ]
@@ -1091,27 +1102,27 @@ impl<'a, const N: usize> Deref for GrantW<'a, N> {
1091
1102
type Target = [ u8 ] ;
1092
1103
1093
1104
fn deref ( & self ) -> & Self :: Target {
1094
- self . buf
1105
+ unsafe { from_raw_parts_mut ( self . buf . as_ptr ( ) as * mut u8 , self . buf . len ( ) ) }
1095
1106
}
1096
1107
}
1097
1108
1098
1109
impl < ' a , const N : usize > DerefMut for GrantW < ' a , N > {
1099
1110
fn deref_mut ( & mut self ) -> & mut [ u8 ] {
1100
- self . buf
1111
+ self . buf ( )
1101
1112
}
1102
1113
}
1103
1114
1104
1115
impl < ' a , const N : usize > Deref for GrantR < ' a , N > {
1105
1116
type Target = [ u8 ] ;
1106
1117
1107
1118
fn deref ( & self ) -> & Self :: Target {
1108
- self . buf
1119
+ self . buf ( )
1109
1120
}
1110
1121
}
1111
1122
1112
1123
impl < ' a , const N : usize > DerefMut for GrantR < ' a , N > {
1113
1124
fn deref_mut ( & mut self ) -> & mut [ u8 ] {
1114
- self . buf
1125
+ self . buf_mut ( )
1115
1126
}
1116
1127
}
1117
1128
0 commit comments