From 115e216067aff0b7b2fa55d9a66394f14a3a8397 Mon Sep 17 00:00:00 2001 From: Rustin-Liu Date: Fri, 24 Apr 2020 22:05:32 +0800 Subject: [PATCH 1/2] Rename AssociatedItems to AssocItems Signed-off-by: Rustin-Liu --- test.rs | 125 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 test.rs diff --git a/test.rs b/test.rs new file mode 100644 index 0000000000000..bc7f5af826efa --- /dev/null +++ b/test.rs @@ -0,0 +1,125 @@ +#![allow(missing_docs)] + +use embedded_hal::digital::v2::{InputPin, OutputPin}; +use generic_array::{ArrayLength, GenericArray}; +use heapless::Vec; + +pub trait HeterogenousArray { + type Len; +} + +/// Macro to implement a iterator on trait objects from a tuple struct. +#[macro_export] +macro_rules! impl_heterogenous_array { + ($s:ident, $t:ty, $len:tt, [$($idx:tt),+]) => { + impl<'a> IntoIterator for &'a $s { + type Item = &'a $t; + type IntoIter = generic_array::GenericArrayIter<&'a $t, $len>; + fn into_iter(self) -> Self::IntoIter { + self.as_array().into_iter() + } + } + impl<'a> IntoIterator for &'a mut $s { + type Item = &'a mut $t; + type IntoIter = generic_array::GenericArrayIter<&'a mut $t, $len>; + fn into_iter(self) -> Self::IntoIter { + self.as_mut_array().into_iter() + } + } + impl $crate::matrix::HeterogenousArray for $s { + type Len = $len; + } + impl $s { + pub fn as_array(&self) -> generic_array::GenericArray<&$t, $len> { + generic_array::arr![&$t; $( &self.$idx as &$t, )+] + } + pub fn as_mut_array(&mut self) -> generic_array::GenericArray<&mut $t, $len> { + generic_array::arr![&mut $t; $( &mut self.$idx as &mut $t, )+] + } + } + } +} + +pub struct Matrix { + cols: C, + rows: R, +} + +impl Matrix { + pub fn new(cols: C, rows: R) -> Result + where + for<'a> &'a mut R: IntoIterator>, + { + let mut res = Self { cols, rows }; + res.clear()?; + Ok(res) + } + pub fn clear<'a, E: 'a>(&'a mut self) -> Result<(), E> + where + &'a mut R: IntoIterator>, + { + for r in self.rows.into_iter() { + r.set_high()?; + } + Ok(()) + } + pub fn get<'a, E: 'a>(&'a mut self) -> Result, E> + where + &'a mut R: IntoIterator>, + R: HeterogenousArray, + R::Len: ArrayLength>, + &'a C: IntoIterator>, + C: HeterogenousArray, + C::Len: ArrayLength, + { + let cols = &self.cols; + self.rows + .into_iter() + .map(|r| { + r.set_low()?; + let col = cols + .into_iter() + .map(|c| c.is_low()) + .collect::, E>>()? + .into_iter() + .collect(); + r.set_high()?; + Ok(col) + }) + .collect::, E>>() + .map(|res| PressedKeys(res.into_iter().collect())) + } +} + +#[derive(Default, PartialEq, Eq)] +pub struct PressedKeys(pub GenericArray, U>) + where + V: ArrayLength, + U: ArrayLength>; + +impl PressedKeys + where + V: ArrayLength, + U: ArrayLength>, +{ + pub fn iter_pressed<'a>(&'a self) -> impl Iterator + Clone + 'a { + self.0.iter().enumerate().flat_map(|(i, r)| { + r.iter() + .enumerate() + .filter_map(move |(j, &b)| if b { Some((i, j)) } else { None }) + }) + } +} + +impl<'a, U, V> IntoIterator for &'a PressedKeys + where + V: ArrayLength, + U: ArrayLength>, + U: ArrayLength<&'a GenericArray>, +{ + type IntoIter = core::slice::Iter<'a, GenericArray>; + type Item = &'a GenericArray; + fn into_iter(self) -> Self::IntoIter { + self.0.iter() + } +} \ No newline at end of file From 6c3f5b85351ba62d4eea875964781cfd924d1ca2 Mon Sep 17 00:00:00 2001 From: hi-rustin Date: Mon, 5 Apr 2021 22:54:50 +0800 Subject: [PATCH 2/2] resolve conflicts resolve conflicts --- compiler/rustc_middle/src/query/mod.rs | 2 +- compiler/rustc_middle/src/ty/assoc.rs | 6 +- compiler/rustc_ty_utils/src/ty.rs | 4 +- .../src/coherence/inherent_impls_overlap.rs | 4 +- test.rs | 125 ------------------ 5 files changed, 8 insertions(+), 133 deletions(-) delete mode 100644 test.rs diff --git a/compiler/rustc_middle/src/query/mod.rs b/compiler/rustc_middle/src/query/mod.rs index bac69e282a521..1aca93fc5c56a 100644 --- a/compiler/rustc_middle/src/query/mod.rs +++ b/compiler/rustc_middle/src/query/mod.rs @@ -564,7 +564,7 @@ rustc_queries! { } /// Collects the associated items defined on a trait or impl. - query associated_items(key: DefId) -> ty::AssociatedItems<'tcx> { + query associated_items(key: DefId) -> ty::AssocItems<'tcx> { storage(ArenaCacheSelector<'tcx>) desc { |tcx| "collecting associated items of {}", tcx.def_path_str(key) } } diff --git a/compiler/rustc_middle/src/ty/assoc.rs b/compiler/rustc_middle/src/ty/assoc.rs index d3770fa416b53..d005f63ed4383 100644 --- a/compiler/rustc_middle/src/ty/assoc.rs +++ b/compiler/rustc_middle/src/ty/assoc.rs @@ -96,15 +96,15 @@ impl AssocKind { /// it is relatively expensive. Instead, items are indexed by `Symbol` and hygienic comparison is /// done only on items with the same name. #[derive(Debug, Clone, PartialEq, HashStable)] -pub struct AssociatedItems<'tcx> { +pub struct AssocItems<'tcx> { pub(super) items: SortedIndexMultiMap, } -impl<'tcx> AssociatedItems<'tcx> { +impl<'tcx> AssocItems<'tcx> { /// Constructs an `AssociatedItems` map from a series of `ty::AssocItem`s in definition order. pub fn new(items_in_def_order: impl IntoIterator) -> Self { let items = items_in_def_order.into_iter().map(|item| (item.ident.name, item)).collect(); - AssociatedItems { items } + AssocItems { items } } /// Returns a slice of associated items in the order they were defined. diff --git a/compiler/rustc_ty_utils/src/ty.rs b/compiler/rustc_ty_utils/src/ty.rs index 29f1761b84d2b..38e5ce6fd831c 100644 --- a/compiler/rustc_ty_utils/src/ty.rs +++ b/compiler/rustc_ty_utils/src/ty.rs @@ -210,9 +210,9 @@ fn associated_item_def_ids(tcx: TyCtxt<'_>, def_id: DefId) -> &[DefId] { } } -fn associated_items(tcx: TyCtxt<'_>, def_id: DefId) -> ty::AssociatedItems<'_> { +fn associated_items(tcx: TyCtxt<'_>, def_id: DefId) -> ty::AssocItems<'_> { let items = tcx.associated_item_def_ids(def_id).iter().map(|did| tcx.associated_item(*did)); - ty::AssociatedItems::new(items) + ty::AssocItems::new(items) } fn def_ident_span(tcx: TyCtxt<'_>, def_id: DefId) -> Option { diff --git a/compiler/rustc_typeck/src/coherence/inherent_impls_overlap.rs b/compiler/rustc_typeck/src/coherence/inherent_impls_overlap.rs index 2965409999202..c69389e7b432b 100644 --- a/compiler/rustc_typeck/src/coherence/inherent_impls_overlap.rs +++ b/compiler/rustc_typeck/src/coherence/inherent_impls_overlap.rs @@ -24,8 +24,8 @@ impl InherentOverlapChecker<'tcx> { /// namespace. fn impls_have_common_items( &self, - impl_items1: &ty::AssociatedItems<'_>, - impl_items2: &ty::AssociatedItems<'_>, + impl_items1: &ty::AssocItems<'_>, + impl_items2: &ty::AssocItems<'_>, ) -> bool { let mut impl_items1 = &impl_items1; let mut impl_items2 = &impl_items2; diff --git a/test.rs b/test.rs deleted file mode 100644 index bc7f5af826efa..0000000000000 --- a/test.rs +++ /dev/null @@ -1,125 +0,0 @@ -#![allow(missing_docs)] - -use embedded_hal::digital::v2::{InputPin, OutputPin}; -use generic_array::{ArrayLength, GenericArray}; -use heapless::Vec; - -pub trait HeterogenousArray { - type Len; -} - -/// Macro to implement a iterator on trait objects from a tuple struct. -#[macro_export] -macro_rules! impl_heterogenous_array { - ($s:ident, $t:ty, $len:tt, [$($idx:tt),+]) => { - impl<'a> IntoIterator for &'a $s { - type Item = &'a $t; - type IntoIter = generic_array::GenericArrayIter<&'a $t, $len>; - fn into_iter(self) -> Self::IntoIter { - self.as_array().into_iter() - } - } - impl<'a> IntoIterator for &'a mut $s { - type Item = &'a mut $t; - type IntoIter = generic_array::GenericArrayIter<&'a mut $t, $len>; - fn into_iter(self) -> Self::IntoIter { - self.as_mut_array().into_iter() - } - } - impl $crate::matrix::HeterogenousArray for $s { - type Len = $len; - } - impl $s { - pub fn as_array(&self) -> generic_array::GenericArray<&$t, $len> { - generic_array::arr![&$t; $( &self.$idx as &$t, )+] - } - pub fn as_mut_array(&mut self) -> generic_array::GenericArray<&mut $t, $len> { - generic_array::arr![&mut $t; $( &mut self.$idx as &mut $t, )+] - } - } - } -} - -pub struct Matrix { - cols: C, - rows: R, -} - -impl Matrix { - pub fn new(cols: C, rows: R) -> Result - where - for<'a> &'a mut R: IntoIterator>, - { - let mut res = Self { cols, rows }; - res.clear()?; - Ok(res) - } - pub fn clear<'a, E: 'a>(&'a mut self) -> Result<(), E> - where - &'a mut R: IntoIterator>, - { - for r in self.rows.into_iter() { - r.set_high()?; - } - Ok(()) - } - pub fn get<'a, E: 'a>(&'a mut self) -> Result, E> - where - &'a mut R: IntoIterator>, - R: HeterogenousArray, - R::Len: ArrayLength>, - &'a C: IntoIterator>, - C: HeterogenousArray, - C::Len: ArrayLength, - { - let cols = &self.cols; - self.rows - .into_iter() - .map(|r| { - r.set_low()?; - let col = cols - .into_iter() - .map(|c| c.is_low()) - .collect::, E>>()? - .into_iter() - .collect(); - r.set_high()?; - Ok(col) - }) - .collect::, E>>() - .map(|res| PressedKeys(res.into_iter().collect())) - } -} - -#[derive(Default, PartialEq, Eq)] -pub struct PressedKeys(pub GenericArray, U>) - where - V: ArrayLength, - U: ArrayLength>; - -impl PressedKeys - where - V: ArrayLength, - U: ArrayLength>, -{ - pub fn iter_pressed<'a>(&'a self) -> impl Iterator + Clone + 'a { - self.0.iter().enumerate().flat_map(|(i, r)| { - r.iter() - .enumerate() - .filter_map(move |(j, &b)| if b { Some((i, j)) } else { None }) - }) - } -} - -impl<'a, U, V> IntoIterator for &'a PressedKeys - where - V: ArrayLength, - U: ArrayLength>, - U: ArrayLength<&'a GenericArray>, -{ - type IntoIter = core::slice::Iter<'a, GenericArray>; - type Item = &'a GenericArray; - fn into_iter(self) -> Self::IntoIter { - self.0.iter() - } -} \ No newline at end of file