From 7f9d7e3a289195c1cbc0e6b1ff7ad04ff28342bd Mon Sep 17 00:00:00 2001 From: mi_sawa Date: Mon, 15 Apr 2024 20:14:05 +0900 Subject: [PATCH 1/2] Allow type with more than one generic arg --- proconio/src/lib.rs | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/proconio/src/lib.rs b/proconio/src/lib.rs index e0d76f6..c61f525 100644 --- a/proconio/src/lib.rs +++ b/proconio/src/lib.rs @@ -604,8 +604,17 @@ macro_rules! input { $crate::input!(@from [$source] @mut [$($mut)*] @var $var @kind [$($kind)*] @rest); $crate::input!(@from [$source] @rest $($rest)*); }; - (@from [$source:expr] @mut [$($mut:tt)?] @var $var:tt @kind [$($kind:tt)*] @rest $tt:tt $($rest:tt)*) => { - $crate::input!(@from [$source] @mut [$($mut)*] @var $var @kind [$($kind)* $tt] @rest $($rest)*); + (@from [$source:expr] @mut [$($mut:tt)?] @var $var:tt @kind [] @rest [$($tt:tt)*] $($rest:tt)*) => { + $crate::input!(@from [$source] @mut [$($mut)*] @var $var @kind [[$($tt)*]] @rest $($rest)*); + }; + (@from [$source:expr] @mut [$($mut:tt)?] @var $var:tt @kind [] @rest ($($tt:tt)*) $($rest:tt)*) => { + $crate::input!(@from [$source] @mut [$($mut)*] @var $var @kind [($($tt)*)] @rest $($rest)*); + }; + (@from [$source:expr] @mut [$($mut:tt)?] @var $var:tt @kind [] @rest $ty:ty, $($rest:tt)*) => { + $crate::input!(@from [$source] @mut [$($mut)*] @var $var @kind [$ty] @rest, $($rest)*); + }; + (@from [$source:expr] @mut [$($mut:tt)?] @var $var:tt @kind [] @rest $ty:ty) => { + $crate::input!(@from [$source] @mut [$($mut)*] @var $var @kind [$ty] @rest); }; (from $source:expr, $($rest:tt)*) => { @@ -980,6 +989,35 @@ mod tests { assert_eq!(xs, [1, 2, 3]); } + #[test] + fn input_generic() { + enum Array { + _Phantom( + std::convert::Infallible, + std::marker::PhantomData T>, + ), + } + + impl crate::source::Readable for Array { + type Output = [T::Output; N]; + + fn read>(source: &mut S) -> Self::Output { + std::array::from_fn(|_| T::read(source)) + } + } + + let source = AutoSource::from("1 2 3 4 5 6 7 8"); + + input! { + from source, + a: Array, + b: Array, + } + + assert_eq!(a, [1, 2, 3, 4, 5]); + assert_eq!(b, [6, 7, 8]); + } + #[test] #[should_panic] fn input_err_different_type() { From 1cd561014feebae9522494bdf2f5ec593491b32f Mon Sep 17 00:00:00 2001 From: mi_sawa Date: Mon, 15 Apr 2024 21:23:20 +0900 Subject: [PATCH 2/2] cargo fmt --- proconio/src/lib.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/proconio/src/lib.rs b/proconio/src/lib.rs index c61f525..8139731 100644 --- a/proconio/src/lib.rs +++ b/proconio/src/lib.rs @@ -1001,7 +1001,9 @@ mod tests { impl crate::source::Readable for Array { type Output = [T::Output; N]; - fn read>(source: &mut S) -> Self::Output { + fn read>( + source: &mut S, + ) -> Self::Output { std::array::from_fn(|_| T::read(source)) } }