rename convert to map

also use the fact that T is known to be Copy
This commit is contained in:
Vinzenz Schroeter 2024-10-15 21:26:20 +02:00
parent bd0ecd77d2
commit 593a975d5c
2 changed files with 13 additions and 7 deletions

View file

@ -129,17 +129,17 @@ mod feature_cp437 {
HashMap::from_iter(pairs) HashMap::from_iter(pairs)
}); });
const MISSING_CHAR_CP437: u8 = 0x3F; const MISSING_CHAR_CP437: u8 = 0x3F; // '?'
impl From<&Cp437Grid> for CharGrid { impl From<&Cp437Grid> for CharGrid {
fn from(value: &Cp437Grid) -> Self { fn from(value: &Cp437Grid) -> Self {
value.convert(move |cp437| cp437_to_char(*cp437)) value.map(cp437_to_char)
} }
} }
impl From<&CharGrid> for Cp437Grid { impl From<&CharGrid> for Cp437Grid {
fn from(value: &CharGrid) -> Self { fn from(value: &CharGrid) -> Self {
value.convert(move |char| char_to_cp437(*char)) value.map(char_to_cp437)
} }
} }

View file

@ -111,13 +111,19 @@ impl<T: PrimitiveGridType> PrimitiveGrid<T> {
} }
} }
/// Convert between PrimitiveGrid types /// Convert between PrimitiveGrid types.
pub fn convert<TConverted, F>(&self, f: F) -> PrimitiveGrid<TConverted> ///
/// See also [Iterator::map].
pub fn map<TConverted, F>(&self, f: F) -> PrimitiveGrid<TConverted>
where where
TConverted: PrimitiveGridType, TConverted: PrimitiveGridType,
F: FnMut(&T) -> TConverted, F: Fn(T) -> TConverted,
{ {
let data = self.data_ref().iter().map(f).collect::<Vec<_>>(); let data = self
.data_ref()
.iter()
.map(|elem| f(*elem))
.collect::<Vec<_>>();
PrimitiveGrid::load(self.width(), self.height(), &data) PrimitiveGrid::load(self.width(), self.height(), &data)
} }
} }