1 | use crate::grid::{ |
2 | config::Entity, |
3 | records::{ExactRecords, Records, RecordsMut}, |
4 | }; |
5 | |
6 | /// A trait for configuring a single cell. |
7 | /// |
8 | /// ~~~~ Where cell represented by 'row' and 'column' indexes. ~~~~ |
9 | /// |
10 | /// A cell can be targeted by [`Cell`]. |
11 | /// |
12 | /// [`Cell`]: crate::object::Cell |
13 | pub trait CellOption<R, C> { |
14 | /// Modification function of a single cell. |
15 | fn change(self, records: &mut R, cfg: &mut C, entity: Entity); |
16 | } |
17 | |
18 | #[cfg (feature = "std" )] |
19 | impl<R, C> CellOption<R, C> for String |
20 | where |
21 | R: Records + ExactRecords + RecordsMut<String>, |
22 | { |
23 | fn change(self, records: &mut R, cfg: &mut C, entity: Entity) { |
24 | (&self).change(records, cfg, entity); |
25 | } |
26 | } |
27 | |
28 | #[cfg (feature = "std" )] |
29 | impl<R, C> CellOption<R, C> for &String |
30 | where |
31 | R: Records + ExactRecords + RecordsMut<String>, |
32 | { |
33 | fn change(self, records: &mut R, _: &mut C, entity: Entity) { |
34 | let count_rows: usize = records.count_rows(); |
35 | let count_cols: usize = records.count_columns(); |
36 | |
37 | for pos: (usize, usize) in entity.iter(count_rows, count_cols) { |
38 | records.set(pos, self.clone()); |
39 | } |
40 | } |
41 | } |
42 | |
43 | impl<'a, R, C> CellOption<R, C> for &'a str |
44 | where |
45 | R: Records + ExactRecords + RecordsMut<&'a str>, |
46 | { |
47 | fn change(self, records: &mut R, _: &mut C, entity: Entity) { |
48 | let count_rows: usize = records.count_rows(); |
49 | let count_cols: usize = records.count_columns(); |
50 | |
51 | for pos: (usize, usize) in entity.iter(count_rows, count_cols) { |
52 | records.set(pos, self); |
53 | } |
54 | } |
55 | } |
56 | |