@@ -878,21 +878,33 @@ where
878
878
879
879
#[ derive( Debug ) ]
880
880
/// A bag of values for different plural cases.
881
- pub struct PluralElements < ' a , T : ?Sized > {
882
- zero : Option < & ' a T > ,
883
- one : Option < & ' a T > ,
884
- two : Option < & ' a T > ,
885
- few : Option < & ' a T > ,
886
- many : Option < & ' a T > ,
887
- other : & ' a T ,
888
- explicit_zero : Option < & ' a T > ,
889
- explicit_one : Option < & ' a T > ,
881
+ pub struct PluralElements < T > ( PluralElementsInner < T > ) ;
882
+
883
+ #[ derive( Debug ) ]
884
+ #[ cfg_attr( feature = "serde" , derive( serde:: Deserialize ) ) ]
885
+ #[ cfg_attr( feature = "datagen" , derive( serde:: Serialize ) ) ]
886
+ pub ( crate ) struct PluralElementsInner < T > {
887
+ #[ cfg_attr( feature = "serde" , serde( skip_serializing_if = "Option::is_none" ) ) ]
888
+ zero : Option < T > ,
889
+ #[ cfg_attr( feature = "serde" , serde( skip_serializing_if = "Option::is_none" ) ) ]
890
+ one : Option < T > ,
891
+ #[ cfg_attr( feature = "serde" , serde( skip_serializing_if = "Option::is_none" ) ) ]
892
+ two : Option < T > ,
893
+ #[ cfg_attr( feature = "serde" , serde( skip_serializing_if = "Option::is_none" ) ) ]
894
+ few : Option < T > ,
895
+ #[ cfg_attr( feature = "serde" , serde( skip_serializing_if = "Option::is_none" ) ) ]
896
+ many : Option < T > ,
897
+ other : T ,
898
+ #[ cfg_attr( feature = "serde" , serde( skip_serializing_if = "Option::is_none" ) ) ]
899
+ explicit_zero : Option < T > ,
900
+ #[ cfg_attr( feature = "serde" , serde( skip_serializing_if = "Option::is_none" ) ) ]
901
+ explicit_one : Option < T > ,
890
902
}
891
903
892
- impl < ' a , T : ? Sized + PartialEq > PluralElements < ' a , T > {
904
+ impl < T > PluralElements < T > {
893
905
/// Creates a new [`PluralElements`] with the given default value.
894
- pub fn new ( other : & ' a T ) -> Self {
895
- Self {
906
+ pub fn new ( other : T ) -> Self {
907
+ Self ( PluralElementsInner {
896
908
other,
897
909
zero : None ,
898
910
one : None ,
@@ -901,102 +913,119 @@ impl<'a, T: ?Sized + PartialEq> PluralElements<'a, T> {
901
913
many : None ,
902
914
explicit_zero : None ,
903
915
explicit_one : None ,
904
- }
916
+ } )
905
917
}
906
918
907
- /// Sets the value for [`PluralCategory::Zero`].
908
- pub fn with_zero_value ( self , zero : Option < & ' a T > ) -> Self {
909
- Self {
910
- zero : zero. filter ( |& t| t != self . other ) ,
911
- ..self
912
- }
919
+ /// The value for [`PluralCategory::Zero`]
920
+ pub fn zero ( & self ) -> & T {
921
+ self . 0 . zero . as_ref ( ) . unwrap_or ( & self . 0 . other )
913
922
}
914
923
915
- /// Sets the value for [`PluralCategory::One`].
916
- pub fn with_one_value ( self , one : Option < & ' a T > ) -> Self {
917
- Self {
918
- one : one. filter ( |& t| t != self . other ) ,
919
- ..self
920
- }
924
+ /// The value for [`PluralCategory::One`]
925
+ pub fn one ( & self ) -> & T {
926
+ self . 0 . one . as_ref ( ) . unwrap_or ( & self . 0 . other )
921
927
}
922
928
923
- /// Sets the value for [`PluralCategory::Two`].
924
- pub fn with_two_value ( self , two : Option < & ' a T > ) -> Self {
925
- Self {
926
- two : two. filter ( |& t| t != self . other ) ,
927
- ..self
928
- }
929
+ /// The value for [`PluralCategory::Two`]
930
+ pub fn two ( & self ) -> & T {
931
+ self . 0 . two . as_ref ( ) . unwrap_or ( & self . 0 . other )
929
932
}
930
933
931
- /// Sets the value for [`PluralCategory::Few`].
932
- pub fn with_few_value ( self , few : Option < & ' a T > ) -> Self {
933
- Self {
934
- few : few. filter ( |& t| t != self . other ) ,
935
- ..self
936
- }
934
+ /// The value for [`PluralCategory::Few`]
935
+ pub fn few ( & self ) -> & T {
936
+ self . 0 . few . as_ref ( ) . unwrap_or ( & self . 0 . other )
937
937
}
938
938
939
- /// Sets the value for [`PluralCategory::Many`].
940
- pub fn with_many_value ( self , many : Option < & ' a T > ) -> Self {
941
- Self {
942
- many : many. filter ( |& t| t != self . other ) ,
943
- ..self
944
- }
939
+ /// The value for [`PluralCategory::Many`]
940
+ pub fn many ( & self ) -> & T {
941
+ self . 0 . many . as_ref ( ) . unwrap_or ( & self . 0 . other )
945
942
}
946
943
947
- /// Sets the value for explicit 0.
948
- pub fn with_explicit_zero_value ( self , explicit_zero : Option < & ' a T > ) -> Self {
949
- Self {
950
- explicit_zero,
951
- ..self
952
- }
944
+ /// The value for [`PluralCategory::Other`]
945
+ pub fn other ( & self ) -> & T {
946
+ & self . 0 . other
953
947
}
954
948
955
- /// Sets the value for explicit 1.
956
- pub fn with_explicit_one_value ( self , explicit_one : Option < & ' a T > ) -> Self {
957
- Self {
958
- explicit_one,
959
- ..self
960
- }
949
+ /// The value used when the [`PluralOperands`] are exactly 0.
950
+ pub fn explicit_zero ( & self ) -> Option < & T > {
951
+ self . 0 . explicit_zero . as_ref ( )
961
952
}
962
953
963
- /// The value for [`PluralCategory::Zero`]
964
- pub fn zero ( & self ) -> & ' a T {
965
- self . zero . unwrap_or ( self . other )
954
+ /// The value used when the [`PluralOperands`] are exactly 1.
955
+ pub fn explicit_one ( & self ) -> Option < & T > {
956
+ self . 0 . explicit_one . as_ref ( )
966
957
}
967
958
968
- /// The value for [`PluralCategory::One`]
969
- pub fn one ( & self ) -> & ' a T {
970
- self . one . unwrap_or ( self . other )
959
+ /// Applies a function `f` to map all values to another type.
960
+ pub fn map < B , F : Fn ( T ) -> B > ( self , f : F ) -> PluralElements < B > {
961
+ let f = & f;
962
+ PluralElements ( PluralElementsInner {
963
+ other : f ( self . 0 . other ) ,
964
+ zero : self . 0 . zero . map ( f) ,
965
+ one : self . 0 . one . map ( f) ,
966
+ two : self . 0 . two . map ( f) ,
967
+ few : self . 0 . few . map ( f) ,
968
+ many : self . 0 . many . map ( f) ,
969
+ explicit_zero : self . 0 . explicit_zero . map ( f) ,
970
+ explicit_one : self . 0 . explicit_one . map ( f) ,
971
+ } )
971
972
}
973
+ }
972
974
973
- /// The value for [`PluralCategory::Two`]
974
- pub fn two ( & self ) -> & ' a T {
975
- self . two . unwrap_or ( self . other )
975
+ impl < T : PartialEq > PluralElements < T > {
976
+ /// Sets the value for [`PluralCategory::Zero`].
977
+ pub fn with_zero_value ( self , zero : Option < T > ) -> Self {
978
+ Self ( PluralElementsInner {
979
+ zero : zero. filter ( |t| * t != self . 0 . other ) ,
980
+ ..self . 0
981
+ } )
976
982
}
977
983
978
- /// The value for [`PluralCategory::Few`]
979
- pub fn few ( & self ) -> & ' a T {
980
- self . few . unwrap_or ( self . other )
984
+ /// Sets the value for [`PluralCategory::One`].
985
+ pub fn with_one_value ( self , one : Option < T > ) -> Self {
986
+ Self ( PluralElementsInner {
987
+ one : one. filter ( |t| * t != self . 0 . other ) ,
988
+ ..self . 0
989
+ } )
981
990
}
982
991
983
- /// The value for [`PluralCategory::Many`]
984
- pub fn many ( & self ) -> & ' a T {
985
- self . many . unwrap_or ( self . other )
992
+ /// Sets the value for [`PluralCategory::Two`].
993
+ pub fn with_two_value ( self , two : Option < T > ) -> Self {
994
+ Self ( PluralElementsInner {
995
+ two : two. filter ( |t| * t != self . 0 . other ) ,
996
+ ..self . 0
997
+ } )
986
998
}
987
999
988
- /// The value for [`PluralCategory::Other`]
989
- pub fn other ( & self ) -> & ' a T {
990
- self . other
1000
+ /// Sets the value for [`PluralCategory::Few`].
1001
+ pub fn with_few_value ( self , few : Option < T > ) -> Self {
1002
+ Self ( PluralElementsInner {
1003
+ few : few. filter ( |t| * t != self . 0 . other ) ,
1004
+ ..self . 0
1005
+ } )
991
1006
}
992
1007
993
- /// The value used when the [`PluralOperands`] are exactly 0.
994
- pub fn explicit_zero ( & self ) -> Option < & ' a T > {
995
- self . explicit_zero
1008
+ /// Sets the value for [`PluralCategory::Many`].
1009
+ pub fn with_many_value ( self , many : Option < T > ) -> Self {
1010
+ Self ( PluralElementsInner {
1011
+ many : many. filter ( |t| * t != self . 0 . other ) ,
1012
+ ..self . 0
1013
+ } )
996
1014
}
997
1015
998
- /// The value used when the [`PluralOperands`] are exactly 1.
999
- pub fn explicit_one ( & self ) -> Option < & ' a T > {
1000
- self . explicit_one
1016
+ /// Sets the value for explicit 0.
1017
+ pub fn with_explicit_zero_value ( self , explicit_zero : Option < T > ) -> Self {
1018
+ Self ( PluralElementsInner {
1019
+ explicit_zero,
1020
+ ..self . 0
1021
+ } )
1022
+ }
1023
+
1024
+ /// Sets the value for explicit 1.
1025
+ pub fn with_explicit_one_value ( self , explicit_one : Option < T > ) -> Self {
1026
+ Self ( PluralElementsInner {
1027
+ explicit_one,
1028
+ ..self . 0
1029
+ } )
1001
1030
}
1002
1031
}
0 commit comments