2020use std:: any:: { Any , TypeId } ;
2121use std:: cell:: { Cell , Ref , RefCell , RefMut } ;
2222use std:: marker:: PhantomData ;
23+ use std:: ops:: DerefMut ;
2324use std:: rc:: { Rc , Weak } ;
2425
2526use crate :: rc:: {
@@ -645,6 +646,19 @@ pub trait Place: Clone {
645646 type Target ;
646647 fn with < R > ( & self , f : impl FnOnce ( & Self :: Target ) -> R ) -> R ;
647648 fn with_mut < R > ( & self , f : impl FnOnce ( & mut Self :: Target ) -> R ) -> R ;
649+
650+ #[ inline]
651+ fn read ( & self ) -> Self :: Target
652+ where
653+ Self :: Target : Clone ,
654+ {
655+ self . with ( Clone :: clone)
656+ }
657+
658+ #[ inline]
659+ fn write ( & self , value : Self :: Target ) {
660+ self . with_mut ( |v| * v = value)
661+ }
648662}
649663
650664impl < T : ByteRepr > Place for Ptr < T > {
@@ -677,40 +691,15 @@ impl<B: Place, T> Clone for FieldPlace<'_, B, T> {
677691
678692impl < B : Place , T > Copy for FieldPlace < ' _ , B , T > { }
679693
680- impl < B : Place , T > FieldPlace < ' _ , B , T > {
681- #[ inline]
682- pub fn with < R > ( & self , f : impl FnOnce ( & T ) -> R ) -> R {
683- self . base . with ( |s| f ( ( self . get ) ( s) ) )
684- }
685-
686- #[ inline]
687- pub fn with_mut < R > ( & self , f : impl FnOnce ( & mut T ) -> R ) -> R {
688- self . base . with_mut ( |s| f ( ( self . get_mut ) ( s) ) )
689- }
690-
691- #[ inline]
692- pub fn read ( & self ) -> T
693- where
694- T : Clone ,
695- {
696- self . with ( T :: clone)
697- }
698-
699- #[ inline]
700- pub fn write ( & self , value : T ) {
701- self . with_mut ( |v| * v = value)
702- }
703- }
704-
705694impl < B : Place , T > Place for FieldPlace < ' _ , B , T > {
706695 type Target = T ;
707696 #[ inline]
708697 fn with < R > ( & self , f : impl FnOnce ( & T ) -> R ) -> R {
709- FieldPlace :: with ( self , f )
698+ self . base . with ( |s| f ( ( self . get ) ( s ) ) )
710699 }
711700 #[ inline]
712701 fn with_mut < R > ( & self , f : impl FnOnce ( & mut T ) -> R ) -> R {
713- FieldPlace :: with_mut ( self , f )
702+ self . base . with_mut ( |s| f ( ( self . get_mut ) ( s ) ) )
714703 }
715704}
716705
@@ -747,44 +736,34 @@ impl<T> Copy for ElemPlace<'_, T> {}
747736
748737impl < T : ByteRepr > ElemPlace < ' _ , T > {
749738 #[ inline]
750- pub fn with < R > ( & self , f : impl FnOnce ( & T ) -> R ) -> R {
751- self . base . with_at ( self . idx , f )
739+ pub fn upgrade ( & self ) -> StrongPtr < T > {
740+ self . base . offset ( self . idx ) . upgrade ( )
752741 }
742+ }
753743
744+ impl < T : ByteRepr > Place for ElemPlace < ' _ , T > {
745+ type Target = T ;
754746 #[ inline]
755- pub fn with_mut < R > ( & self , f : impl FnOnce ( & mut T ) -> R ) -> R {
747+ fn with < R > ( & self , f : impl FnOnce ( & T ) -> R ) -> R {
748+ self . base . with_at ( self . idx , f)
749+ }
750+ #[ inline]
751+ fn with_mut < R > ( & self , f : impl FnOnce ( & mut T ) -> R ) -> R {
756752 self . base . with_mut_at ( self . idx , f)
757753 }
758-
754+ // Elements are read and written without the extra call of with, which
755+ // keeps them inlined in hot loops.
759756 #[ inline]
760- pub fn read ( & self ) -> T
757+ fn read ( & self ) -> T
761758 where
762759 T : Clone ,
763760 {
764761 self . base . read_at ( self . idx )
765762 }
766-
767763 #[ inline]
768- pub fn write ( & self , value : T ) {
764+ fn write ( & self , value : T ) {
769765 self . base . write_at ( self . idx , value)
770766 }
771-
772- #[ inline]
773- pub fn upgrade ( & self ) -> StrongPtr < T > {
774- self . base . offset ( self . idx ) . upgrade ( )
775- }
776- }
777-
778- impl < T : ByteRepr > Place for ElemPlace < ' _ , T > {
779- type Target = T ;
780- #[ inline]
781- fn with < R > ( & self , f : impl FnOnce ( & T ) -> R ) -> R {
782- ElemPlace :: with ( self , f)
783- }
784- #[ inline]
785- fn with_mut < R > ( & self , f : impl FnOnce ( & mut T ) -> R ) -> R {
786- ElemPlace :: with_mut ( self , f)
787- }
788767}
789768
790769// Element `idx` of the sequence that pointer `base` points into, e.g.,
@@ -801,37 +780,6 @@ macro_rules! elem {
801780 } ;
802781}
803782
804- // An array or vector, whose elements are indexed by IndexPlace.
805- pub trait Seq {
806- type Elem ;
807- fn elems ( & self ) -> & [ Self :: Elem ] ;
808- fn elems_mut ( & mut self ) -> & mut [ Self :: Elem ] ;
809- }
810-
811- impl < T > Seq for Box < [ T ] > {
812- type Elem = T ;
813- #[ inline]
814- fn elems ( & self ) -> & [ T ] {
815- self
816- }
817- #[ inline]
818- fn elems_mut ( & mut self ) -> & mut [ T ] {
819- self
820- }
821- }
822-
823- impl < T > Seq for Vec < T > {
824- type Elem = T ;
825- #[ inline]
826- fn elems ( & self ) -> & [ T ] {
827- self
828- }
829- #[ inline]
830- fn elems_mut ( & mut self ) -> & mut [ T ] {
831- self
832- }
833- }
834-
835783// Element `idx` of the array or vector in place `base`, e.g., a row of an
836784// array field of a struct reached through a pointer. Written by
837785// `index!(place, i)`. It is a struct expression, like elem!.
@@ -849,53 +797,26 @@ impl<B> Clone for IndexPlace<'_, B> {
849797
850798impl < B > Copy for IndexPlace < ' _ , B > { }
851799
852- impl < B : Place > IndexPlace < ' _ , B >
853- where
854- B :: Target : Seq ,
855- {
856- #[ inline]
857- pub fn with < R > ( & self , f : impl FnOnce ( & <B :: Target as Seq >:: Elem ) -> R ) -> R {
858- self . base . with ( |c| f ( & c. elems ( ) [ self . idx ] ) )
859- }
860-
861- #[ inline]
862- pub fn with_mut < R > ( & self , f : impl FnOnce ( & mut <B :: Target as Seq >:: Elem ) -> R ) -> R {
863- self . base . with_mut ( |c| f ( & mut c. elems_mut ( ) [ self . idx ] ) )
864- }
865-
866- #[ inline]
867- pub fn read ( & self ) -> <B :: Target as Seq >:: Elem
868- where
869- <B :: Target as Seq >:: Elem : Clone ,
870- {
871- self . with ( Clone :: clone)
872- }
873-
874- #[ inline]
875- pub fn write ( & self , value : <B :: Target as Seq >:: Elem ) {
876- self . with_mut ( |v| * v = value)
877- }
878- }
879-
880800impl < T : ByteRepr > IndexPlace < ' _ , Ptr < Vec < T > > > {
881801 #[ inline]
882802 pub fn upgrade ( & self ) -> StrongPtr < T > {
883803 self . base . decay ( ) . offset ( self . idx ) . upgrade ( )
884804 }
885805}
886806
887- impl < B : Place > Place for IndexPlace < ' _ , B >
807+ // The array or vector is a slice of its elements.
808+ impl < B : Place , E > Place for IndexPlace < ' _ , B >
888809where
889- B :: Target : Seq ,
810+ B :: Target : DerefMut < Target = [ E ] > ,
890811{
891- type Target = < B :: Target as Seq > :: Elem ;
812+ type Target = E ;
892813 #[ inline]
893- fn with < R > ( & self , f : impl FnOnce ( & Self :: Target ) -> R ) -> R {
894- IndexPlace :: with ( self , f )
814+ fn with < R > ( & self , f : impl FnOnce ( & E ) -> R ) -> R {
815+ self . base . with ( |c| f ( & c [ self . idx ] ) )
895816 }
896817 #[ inline]
897- fn with_mut < R > ( & self , f : impl FnOnce ( & mut Self :: Target ) -> R ) -> R {
898- IndexPlace :: with_mut ( self , f )
818+ fn with_mut < R > ( & self , f : impl FnOnce ( & mut E ) -> R ) -> R {
819+ self . base . with_mut ( |c| f ( & mut c [ self . idx ] ) )
899820 }
900821}
901822
@@ -942,36 +863,6 @@ impl<S: Record, T: ByteRepr> ArrayElem<'_, S, T> {
942863 . offset ( self . idx )
943864 }
944865
945- #[ inline]
946- pub fn with < R > ( & self , f : impl FnOnce ( & T ) -> R ) -> R {
947- if let PtrKind :: Reinterpreted ( _) = self . base . kind {
948- return self . ptr ( ) . with ( f) ;
949- }
950- self . base . with ( |s| f ( & ( self . get ) ( s) [ self . idx as usize ] ) )
951- }
952-
953- #[ inline]
954- pub fn with_mut < R > ( & self , f : impl FnOnce ( & mut T ) -> R ) -> R {
955- if let PtrKind :: Reinterpreted ( _) = self . base . kind {
956- return self . ptr ( ) . with_mut ( f) ;
957- }
958- self . base
959- . with_mut ( |s| f ( & mut ( self . get_mut ) ( s) [ self . idx as usize ] ) )
960- }
961-
962- #[ inline]
963- pub fn read ( & self ) -> T
964- where
965- T : Clone ,
966- {
967- self . with ( T :: clone)
968- }
969-
970- #[ inline]
971- pub fn write ( & self , value : T ) {
972- self . with_mut ( |v| * v = value)
973- }
974-
975866 #[ inline]
976867 pub fn upgrade ( & self ) -> StrongPtr < T > {
977868 self . ptr ( ) . upgrade ( )
@@ -982,11 +873,18 @@ impl<S: Record, T: ByteRepr> Place for ArrayElem<'_, S, T> {
982873 type Target = T ;
983874 #[ inline]
984875 fn with < R > ( & self , f : impl FnOnce ( & T ) -> R ) -> R {
985- ArrayElem :: with ( self , f)
876+ if let PtrKind :: Reinterpreted ( _) = self . base . kind {
877+ return self . ptr ( ) . with ( f) ;
878+ }
879+ self . base . with ( |s| f ( & ( self . get ) ( s) [ self . idx as usize ] ) )
986880 }
987881 #[ inline]
988882 fn with_mut < R > ( & self , f : impl FnOnce ( & mut T ) -> R ) -> R {
989- ArrayElem :: with_mut ( self , f)
883+ if let PtrKind :: Reinterpreted ( _) = self . base . kind {
884+ return self . ptr ( ) . with_mut ( f) ;
885+ }
886+ self . base
887+ . with_mut ( |s| f ( & mut ( self . get_mut ) ( s) [ self . idx as usize ] ) )
990888 }
991889}
992890
@@ -1026,29 +924,6 @@ impl<S: Record, T> Clone for VecElem<'_, S, T> {
1026924impl < S : Record , T > Copy for VecElem < ' _ , S , T > { }
1027925
1028926impl < S : Record , T : ByteRepr > VecElem < ' _ , S , T > {
1029- #[ inline]
1030- pub fn with < R > ( & self , f : impl FnOnce ( & T ) -> R ) -> R {
1031- self . base . with ( |s| f ( & ( self . get ) ( s) [ self . idx ] ) )
1032- }
1033-
1034- #[ inline]
1035- pub fn with_mut < R > ( & self , f : impl FnOnce ( & mut T ) -> R ) -> R {
1036- self . base . with_mut ( |s| f ( & mut ( self . get_mut ) ( s) [ self . idx ] ) )
1037- }
1038-
1039- #[ inline]
1040- pub fn read ( & self ) -> T
1041- where
1042- T : Clone ,
1043- {
1044- self . with ( T :: clone)
1045- }
1046-
1047- #[ inline]
1048- pub fn write ( & self , value : T ) {
1049- self . with_mut ( |v| * v = value)
1050- }
1051-
1052927 #[ inline]
1053928 pub fn upgrade ( & self ) -> StrongPtr < T > {
1054929 let elems: Ptr < T > = self
@@ -1063,11 +938,11 @@ impl<S: Record, T: ByteRepr> Place for VecElem<'_, S, T> {
1063938 type Target = T ;
1064939 #[ inline]
1065940 fn with < R > ( & self , f : impl FnOnce ( & T ) -> R ) -> R {
1066- VecElem :: with ( self , f )
941+ self . base . with ( |s| f ( & ( self . get ) ( s ) [ self . idx ] ) )
1067942 }
1068943 #[ inline]
1069944 fn with_mut < R > ( & self , f : impl FnOnce ( & mut T ) -> R ) -> R {
1070- VecElem :: with_mut ( self , f )
945+ self . base . with_mut ( |s| f ( & mut ( self . get_mut ) ( s ) [ self . idx ] ) )
1071946 }
1072947}
1073948
@@ -1273,7 +1148,7 @@ pub(crate) fn upgrade(root: &Weak<dyn Root>) -> Rc<dyn Root> {
12731148
12741149#[ cfg( test) ]
12751150mod tests {
1276- use crate :: { AsPointer , ByteRepr , Decay , Ptr , Record , Value } ;
1151+ use crate :: { AsPointer , ByteRepr , Decay , Place , Ptr , Record , Value } ;
12771152 use std:: cell:: RefCell ;
12781153 use std:: rc:: Rc ;
12791154
@@ -1550,10 +1425,7 @@ mod tests {
15501425 name. offset ( 1 )
15511426 . with_slice_mut ( 2 , |b| b. copy_from_slice ( b"xy" ) ) ;
15521427 assert_eq ! ( & s. borrow( ) . name[ ..4 ] , b"axy\0 " ) ;
1553- assert_eq ! ( name. slice_until( & name. offset( 3 ) ) , b"axy" . to_vec( ) ) ;
15541428 let nums: Ptr < i32 > = field_ptr ! ( s, nums) . decay ( ) ;
1555- nums. sort ( 3 ) ;
1556- assert_eq ! ( s. borrow( ) . nums, vec![ 1 , 2 , 3 ] ) ;
15571429 assert_eq ! ( nums. len( ) , 3 ) ;
15581430 }
15591431
0 commit comments