Skip to content

Commit 8fb0d53

Browse files
committed
rust: init: Support type paths in pin_init!() and friends
This makes directly initializing structures with a type name that isn't in the current scope work properly. Signed-off-by: Asahi Lina <[email protected]>
1 parent cc4e112 commit 8fb0d53

1 file changed

Lines changed: 46 additions & 46 deletions

File tree

rust/kernel/init.rs

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -541,12 +541,12 @@ macro_rules! stack_try_pin_init {
541541
// module `__internal` inside of `init/__internal.rs`.
542542
#[macro_export]
543543
macro_rules! pin_init {
544-
($(&$this:ident in)? $t:ident $(::<$($generics:ty),* $(,)?>)? {
544+
($(&$this:ident in)? $t:ident $(::$p:ident)* $(::<$($generics:ty),* $(,)?>)? {
545545
$($fields:tt)*
546546
}) => {
547547
$crate::try_pin_init!(parse_zeroable_end:
548548
@this($($this)?),
549-
@typ($t $(::<$($generics),*>)?),
549+
@typ($t $(::$p)* $(::<$($generics),*>)?),
550550
@fields($($fields)*),
551551
@error(::core::convert::Infallible),
552552
@munch_fields($($fields)*),
@@ -595,76 +595,76 @@ macro_rules! pin_init {
595595
// module `__internal` inside of `init/__internal.rs`.
596596
#[macro_export]
597597
macro_rules! try_pin_init {
598-
($(&$this:ident in)? $t:ident $(::<$($generics:ty),* $(,)?>)? {
598+
($(&$this:ident in)? $t:ident $(::$p:ident)* $(::<$($generics:ty),* $(,)?>)? {
599599
$($fields:tt)*
600600
}) => {
601601
$crate::try_pin_init!(parse_zeroable_end:
602602
@this($($this)?),
603-
@typ($t $(::<$($generics),*>)? ),
603+
@typ($t $(::$p)* $(::<$($generics),*>)? ),
604604
@fields($($fields)*),
605605
@error($crate::error::Error),
606606
@munch_fields($($fields)*),
607607
)
608608
};
609-
($(&$this:ident in)? $t:ident $(::<$($generics:ty),* $(,)?>)? {
609+
($(&$this:ident in)? $t:ident $(::$p:ident)* $(::<$($generics:ty),* $(,)?>)? {
610610
$($fields:tt)*
611611
}? $err:ty) => {
612612
$crate::try_pin_init!(parse_zeroable_end:
613613
@this($($this)?),
614-
@typ($t $(::<$($generics),*>)? ),
614+
@typ($t $(::$p)* $(::<$($generics),*>)? ),
615615
@fields($($fields)*),
616616
@error($err),
617617
@munch_fields($($fields)*),
618618
)
619619
};
620620
(parse_zeroable_end:
621621
@this($($this:ident)?),
622-
@typ($t:ident $(::<$($generics:ty),*>)?),
622+
@typ($t:ident $(::$p:ident)* $(::<$($generics:ty),*>)?),
623623
@fields($($fields:tt)*),
624624
@error($err:ty),
625625
@munch_fields(),
626626
) => {
627627
$crate::try_pin_init!(
628628
@this($($this)?),
629-
@typ($t $(::<$($generics),*>)?),
629+
@typ($t $(::$p)* $(::<$($generics),*>)?),
630630
@fields($($fields)*),
631631
@error($err),
632632
@zeroed(), // nothing means we do not zero unmentioned fields
633633
)
634634
};
635635
(parse_zeroable_end:
636636
@this($($this:ident)?),
637-
@typ($t:ident $(::<$($generics:ty),*>)?),
637+
@typ($t:ident $(::$p:ident)* $(::<$($generics:ty),*>)?),
638638
@fields($($fields:tt)*),
639639
@error($err:ty),
640640
@munch_fields(..Zeroable::zeroed()),
641641
) => {
642642
$crate::try_pin_init!(
643643
@this($($this)?),
644-
@typ($t $(::<$($generics),*>)?),
644+
@typ($t $(::$p)* $(::<$($generics),*>)?),
645645
@fields($($fields)*),
646646
@error($err),
647647
@zeroed(()), // () means we zero unmentioned fields
648648
)
649649
};
650650
(parse_zeroable_end:
651651
@this($($this:ident)?),
652-
@typ($t:ident $(::<$($generics:ty),*>)?),
652+
@typ($t:ident $(::$p:ident)* $(::<$($generics:ty),*>)?),
653653
@fields($($fields:tt)*),
654654
@error($err:ty),
655655
@munch_fields($ignore:tt $($rest:tt)*),
656656
) => {
657657
$crate::try_pin_init!(parse_zeroable_end:
658658
@this($($this)?),
659-
@typ($t $(::<$($generics),*>)?),
659+
@typ($t $(::$p)* $(::<$($generics),*>)?),
660660
@fields($($fields)*),
661661
@error($err),
662662
@munch_fields($($rest)*),
663663
)
664664
};
665665
(
666666
@this($($this:ident)?),
667-
@typ($t:ident $(::<$($generics:ty),*>)?),
667+
@typ($t:ident $(::$p:ident)* $(::<$($generics:ty),*>)?),
668668
@fields($($fields:tt)*),
669669
@error($err:ty),
670670
@zeroed($($init_zeroed:expr)?),
@@ -676,7 +676,7 @@ macro_rules! try_pin_init {
676676
// Get the pin data from the supplied type.
677677
let data = unsafe {
678678
use $crate::init::__internal::HasPinData;
679-
$t$(::<$($generics),*>)?::__pin_data()
679+
$t$(::$p)*$(::<$($generics),*>)?::__pin_data()
680680
};
681681
// Ensure that `data` really is of type `PinData` and help with type inference:
682682
let init = $crate::init::__internal::PinData::make_closure::<_, __InitOk, $err>(
@@ -716,7 +716,7 @@ macro_rules! try_pin_init {
716716
(|| {
717717
$crate::try_pin_init!(make_initializer:
718718
@slot(slot),
719-
@type_name($t),
719+
@type_name($t$(::$p)*),
720720
@munch_fields($($fields)*,),
721721
@acc(),
722722
);
@@ -802,7 +802,7 @@ macro_rules! try_pin_init {
802802
};
803803
(make_initializer:
804804
@slot($slot:ident),
805-
@type_name($t:ident),
805+
@type_name($t:ident $(::$p:ident)*),
806806
@munch_fields(..Zeroable::zeroed() $(,)?),
807807
@acc($($acc:tt)*),
808808
) => {
@@ -816,49 +816,49 @@ macro_rules! try_pin_init {
816816
let mut zeroed = ::core::mem::zeroed();
817817
::core::ptr::write($slot, zeroed);
818818
zeroed = ::core::mem::zeroed();
819-
::core::ptr::write($slot, $t {
819+
::core::ptr::write($slot, $t$(::$p)* {
820820
$($acc)*
821821
..zeroed
822822
});
823823
}
824824
};
825825
(make_initializer:
826826
@slot($slot:ident),
827-
@type_name($t:ident),
827+
@type_name($t:ident $(::$p:ident)*),
828828
@munch_fields($(,)?),
829829
@acc($($acc:tt)*),
830830
) => {
831831
// Endpoint, nothing more to munch, create the initializer.
832832
// Since we are in the `if false` branch, this will never get executed. We abuse `slot` to
833833
// get the correct type inference here:
834834
unsafe {
835-
::core::ptr::write($slot, $t {
835+
::core::ptr::write($slot, $t$(::$p)* {
836836
$($acc)*
837837
});
838838
}
839839
};
840840
(make_initializer:
841841
@slot($slot:ident),
842-
@type_name($t:ident),
842+
@type_name($t:ident $(::$p:ident)*),
843843
@munch_fields($field:ident <- $val:expr, $($rest:tt)*),
844844
@acc($($acc:tt)*),
845845
) => {
846846
$crate::try_pin_init!(make_initializer:
847847
@slot($slot),
848-
@type_name($t),
848+
@type_name($t$(::$p)*),
849849
@munch_fields($($rest)*),
850850
@acc($($acc)* $field: ::core::panic!(),),
851851
);
852852
};
853853
(make_initializer:
854854
@slot($slot:ident),
855-
@type_name($t:ident),
855+
@type_name($t:ident $(::$p:ident)*),
856856
@munch_fields($field:ident $(: $val:expr)?, $($rest:tt)*),
857857
@acc($($acc:tt)*),
858858
) => {
859859
$crate::try_pin_init!(make_initializer:
860860
@slot($slot),
861-
@type_name($t),
861+
@type_name($t$(::$p)*),
862862
@munch_fields($($rest)*),
863863
@acc($($acc)* $field: ::core::panic!(),),
864864
);
@@ -882,12 +882,12 @@ macro_rules! try_pin_init {
882882
// module `__internal` inside of `init/__internal.rs`.
883883
#[macro_export]
884884
macro_rules! init {
885-
($(&$this:ident in)? $t:ident $(::<$($generics:ty),* $(,)?>)? {
885+
($(&$this:ident in)? $t:ident $(::$p:ident)* $(::<$($generics:ty),* $(,)?>)? {
886886
$($fields:tt)*
887887
}) => {
888888
$crate::try_init!(parse_zeroable_end:
889889
@this($($this)?),
890-
@typ($t $(::<$($generics),*>)?),
890+
@typ($t $(::$p)* $(::<$($generics),*>)?),
891891
@fields($($fields)*),
892892
@error(::core::convert::Infallible),
893893
@munch_fields($($fields)*),
@@ -930,76 +930,76 @@ macro_rules! init {
930930
// module `__internal` inside of `init/__internal.rs`.
931931
#[macro_export]
932932
macro_rules! try_init {
933-
($(&$this:ident in)? $t:ident $(::<$($generics:ty),* $(,)?>)? {
933+
($(&$this:ident in)? $t:ident $(::$p:ident)* $(::<$($generics:ty),* $(,)?>)? {
934934
$($fields:tt)*
935935
}) => {
936936
$crate::try_init!(parse_zeroable_end:
937937
@this($($this)?),
938-
@typ($t $(::<$($generics),*>)?),
938+
@typ($t $(::$p)* $(::<$($generics),*>)?),
939939
@fields($($fields)*),
940940
@error($crate::error::Error),
941941
@munch_fields($($fields)*),
942942
)
943943
};
944-
($(&$this:ident in)? $t:ident $(::<$($generics:ty),* $(,)?>)? {
944+
($(&$this:ident in)? $t:ident $(::$p:ident)* $(::<$($generics:ty),* $(,)?>)? {
945945
$($fields:tt)*
946946
}? $err:ty) => {
947947
$crate::try_init!(parse_zeroable_end:
948948
@this($($this)?),
949-
@typ($t $(::<$($generics),*>)?),
949+
@typ($t $(::$p)* $(::<$($generics),*>)?),
950950
@fields($($fields)*),
951951
@error($err),
952952
@munch_fields($($fields)*),
953953
)
954954
};
955955
(parse_zeroable_end:
956956
@this($($this:ident)?),
957-
@typ($t:ident $(::<$($generics:ty),*>)?),
957+
@typ($t:ident $(::$p:ident)* $(::<$($generics:ty),*>)?),
958958
@fields($($fields:tt)*),
959959
@error($err:ty),
960960
@munch_fields(),
961961
) => {
962962
$crate::try_init!(
963963
@this($($this)?),
964-
@typ($t $(::<$($generics),*>)?),
964+
@typ($t $(::$p)* $(::<$($generics),*>)?),
965965
@fields($($fields)*),
966966
@error($err),
967967
@zeroed(), // Nothing means we do not zero unmentioned fields.
968968
)
969969
};
970970
(parse_zeroable_end:
971971
@this($($this:ident)?),
972-
@typ($t:ident $(::<$($generics:ty),*>)?),
972+
@typ($t:ident $(::$p:ident)* $(::<$($generics:ty),*>)?),
973973
@fields($($fields:tt)*),
974974
@error($err:ty),
975975
@munch_fields(..Zeroable::zeroed()),
976976
) => {
977977
$crate::try_init!(
978978
@this($($this)?),
979-
@typ($t $(::<$($generics),*>)?),
979+
@typ($t $(::$p)* $(::<$($generics),*>)?),
980980
@fields($($fields)*),
981981
@error($err),
982982
@zeroed(()), // () means we zero unmentioned fields.
983983
)
984984
};
985985
(parse_zeroable_end:
986986
@this($($this:ident)?),
987-
@typ($t:ident $(::<$($generics:ty),*>)?),
987+
@typ($t:ident $(::$p:ident)* $(::<$($generics:ty),*>)?),
988988
@fields($($fields:tt)*),
989989
@error($err:ty),
990990
@munch_fields($ignore:tt $($rest:tt)*),
991991
) => {
992992
$crate::try_init!(parse_zeroable_end:
993993
@this($($this)?),
994-
@typ($t $(::<$($generics),*>)?),
994+
@typ($t $(::$p)* $(::<$($generics),*>)?),
995995
@fields($($fields)*),
996996
@error($err),
997997
@munch_fields($($rest)*),
998998
)
999999
};
10001000
(
10011001
@this($($this:ident)?),
1002-
@typ($t:ident $(::<$($generics:ty),*>)?),
1002+
@typ($t:ident $(::$p:ident)* $(::<$($generics:ty),*>)?),
10031003
@fields($($fields:tt)*),
10041004
@error($err:ty),
10051005
@zeroed($($init_zeroed:expr)?),
@@ -1011,7 +1011,7 @@ macro_rules! try_init {
10111011
// Get the init data from the supplied type.
10121012
let data = unsafe {
10131013
use $crate::init::__internal::HasInitData;
1014-
$t$(::<$($generics),*>)?::__init_data()
1014+
$t$(::$p)*$(::<$($generics),*>)?::__init_data()
10151015
};
10161016
// Ensure that `data` really is of type `InitData` and help with type inference:
10171017
let init = $crate::init::__internal::InitData::make_closure::<_, __InitOk, $err>(
@@ -1050,7 +1050,7 @@ macro_rules! try_init {
10501050
(|| {
10511051
$crate::try_init!(make_initializer:
10521052
@slot(slot),
1053-
@type_name($t),
1053+
@type_name($t$(::$p)*),
10541054
@munch_fields($($fields)*,),
10551055
@acc(),
10561056
);
@@ -1135,7 +1135,7 @@ macro_rules! try_init {
11351135
};
11361136
(make_initializer:
11371137
@slot($slot:ident),
1138-
@type_name($t:ident),
1138+
@type_name($t:ident $(::$p:ident)*),
11391139
@munch_fields(..Zeroable::zeroed() $(,)?),
11401140
@acc($($acc:tt)*),
11411141
) => {
@@ -1149,49 +1149,49 @@ macro_rules! try_init {
11491149
let mut zeroed = ::core::mem::zeroed();
11501150
::core::ptr::write($slot, zeroed);
11511151
zeroed = ::core::mem::zeroed();
1152-
::core::ptr::write($slot, $t {
1152+
::core::ptr::write($slot, $t$(::$p)* {
11531153
$($acc)*
11541154
..zeroed
11551155
});
11561156
}
11571157
};
11581158
(make_initializer:
11591159
@slot($slot:ident),
1160-
@type_name($t:ident),
1160+
@type_name($t:ident $(::$p:ident)*),
11611161
@munch_fields($(,)?),
11621162
@acc($($acc:tt)*),
11631163
) => {
11641164
// Endpoint, nothing more to munch, create the initializer.
11651165
// Since we are in the `if false` branch, this will never get executed. We abuse `slot` to
11661166
// get the correct type inference here:
11671167
unsafe {
1168-
::core::ptr::write($slot, $t {
1168+
::core::ptr::write($slot, $t$(::$p)* {
11691169
$($acc)*
11701170
});
11711171
}
11721172
};
11731173
(make_initializer:
11741174
@slot($slot:ident),
1175-
@type_name($t:ident),
1175+
@type_name($t:ident $(::$p:ident)*),
11761176
@munch_fields($field:ident <- $val:expr, $($rest:tt)*),
11771177
@acc($($acc:tt)*),
11781178
) => {
11791179
$crate::try_init!(make_initializer:
11801180
@slot($slot),
1181-
@type_name($t),
1181+
@type_name($t$(::$p)*),
11821182
@munch_fields($($rest)*),
11831183
@acc($($acc)*$field: ::core::panic!(),),
11841184
);
11851185
};
11861186
(make_initializer:
11871187
@slot($slot:ident),
1188-
@type_name($t:ident),
1188+
@type_name($t:ident $(::$p:ident)*),
11891189
@munch_fields($field:ident $(: $val:expr)?, $($rest:tt)*),
11901190
@acc($($acc:tt)*),
11911191
) => {
11921192
$crate::try_init!(make_initializer:
11931193
@slot($slot),
1194-
@type_name($t),
1194+
@type_name($t$(::$p)*),
11951195
@munch_fields($($rest)*),
11961196
@acc($($acc)*$field: ::core::panic!(),),
11971197
);

0 commit comments

Comments
 (0)