Skip to content

Commit 9d31588

Browse files
committed
Add a lifetime parameter to MatrixConfig.
This is needed so that get_row_pin/get_column_pin's return value can borrow from &self. This could be solved more ergonomically using a HKT constructor, as defined in this Rust RFC: rust-lang/rfcs#1598
1 parent 64cdbe2 commit 9d31588

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

src/matrix_config.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
pub struct RowPins<'a, MC: MatrixConfig + 'a> {
1+
pub struct RowPins<'a, MC: MatrixConfig<'a> + 'a> {
22
count: usize,
33
conf: &'a MC,
44
}
55

6-
impl<'a, MC: MatrixConfig + 'a> RowPins<'a, MC> {
6+
impl<'a, MC: MatrixConfig<'a>> RowPins<'a, MC> {
77
fn new(conf: &'a MC) -> RowPins<'a, MC> {
88
RowPins {
99
count: 0,
@@ -12,7 +12,7 @@ impl<'a, MC: MatrixConfig + 'a> RowPins<'a, MC> {
1212
}
1313
}
1414

15-
impl<'a, MC: MatrixConfig> Iterator for RowPins<'a, MC> {
15+
impl<'a, MC: MatrixConfig<'a>> Iterator for RowPins<'a, MC> {
1616
type Item = MC::InputPin;
1717
fn next(&mut self) -> Option<Self::Item> {
1818
if self.count == self.conf.get_num_rows() {
@@ -25,12 +25,12 @@ impl<'a, MC: MatrixConfig> Iterator for RowPins<'a, MC> {
2525
}
2626
}
2727

28-
pub struct ColumnPins<'a, MC: MatrixConfig + 'a> {
28+
pub struct ColumnPins<'a, MC: MatrixConfig<'a> + 'a> {
2929
count: usize,
3030
conf: &'a MC,
3131
}
3232

33-
impl<'a, MC: MatrixConfig + 'a> ColumnPins<'a, MC> {
33+
impl<'a, MC: MatrixConfig<'a>> ColumnPins<'a, MC> {
3434
fn new(conf: &'a MC) -> ColumnPins<'a, MC> {
3535
ColumnPins {
3636
count: 0,
@@ -39,7 +39,7 @@ impl<'a, MC: MatrixConfig + 'a> ColumnPins<'a, MC> {
3939
}
4040
}
4141

42-
impl<'a, MC: MatrixConfig> Iterator for ColumnPins<'a, MC> {
42+
impl<'a, MC: MatrixConfig<'a>> Iterator for ColumnPins<'a, MC> {
4343
type Item = MC::OutputPin;
4444
fn next(&mut self) -> Option<Self::Item> {
4545
if self.count == self.conf.get_num_columns() {
@@ -52,22 +52,22 @@ impl<'a, MC: MatrixConfig> Iterator for ColumnPins<'a, MC> {
5252
}
5353
}
5454

55-
pub trait MatrixConfig {
55+
pub trait MatrixConfig<'a> {
5656
type InputPin;
5757
type OutputPin;
5858

5959
fn get_num_rows(&self) -> usize;
6060
fn get_num_columns(&self) -> usize;
6161

62-
fn get_row_pin(&self, idx: usize) -> Self::InputPin;
63-
fn get_column_pin(&self, idx: usize) -> Self::OutputPin;
62+
fn get_row_pin(&'a self, idx: usize) -> Self::InputPin;
63+
fn get_column_pin(&'a self, idx: usize) -> Self::OutputPin;
6464

65-
fn rows(&self) -> RowPins<Self>
65+
fn rows(&'a self) -> RowPins<Self>
6666
where Self: Sized
6767
{
6868
RowPins::new(&self)
6969
}
70-
fn columns(&self) -> ColumnPins<Self>
70+
fn columns(&'a self) -> ColumnPins<Self>
7171
where Self: Sized
7272
{
7373
ColumnPins::new(&self)
@@ -80,7 +80,7 @@ mod tests {
8080
#[test]
8181
fn basic() {
8282
struct TestMatrixConfig {}
83-
impl MatrixConfig for TestMatrixConfig {
83+
impl<'a> MatrixConfig<'a> for TestMatrixConfig {
8484
type InputPin = u32;
8585
type OutputPin = u32;
8686
fn get_num_rows(&self) -> usize {
@@ -89,10 +89,10 @@ mod tests {
8989
fn get_num_columns(&self) -> usize {
9090
3
9191
}
92-
fn get_row_pin(&self, idx: usize) -> Self::InputPin {
92+
fn get_row_pin(&'a self, idx: usize) -> Self::InputPin {
9393
idx as u32
9494
}
95-
fn get_column_pin(&self, idx: usize) -> Self::OutputPin {
95+
fn get_column_pin(&'a self, idx: usize) -> Self::OutputPin {
9696
idx as u32
9797
}
9898
}

src/scan.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub trait OutputPin {
1010
fn set_high(&self);
1111
}
1212

13-
pub fn single_scan<MC: MatrixConfig, RM: Matrix<u32>>(conf: &MC) -> RM
13+
pub fn single_scan<'a, MC: MatrixConfig<'a>, RM: Matrix<u32>>(conf: &'a MC) -> RM
1414
where MC::InputPin: InputPin,
1515
MC::OutputPin: OutputPin
1616
{
@@ -38,7 +38,7 @@ pub fn private_basic() {
3838
fn set_low(&self) {}
3939
fn set_high(&self) {}
4040
}
41-
impl MatrixConfig for TestMatrixConfig {
41+
impl<'a> MatrixConfig<'a> for TestMatrixConfig {
4242
type InputPin = TestPin;
4343
type OutputPin = TestPin;
4444
fn get_num_rows(&self) -> usize {
@@ -47,10 +47,10 @@ pub fn private_basic() {
4747
fn get_num_columns(&self) -> usize {
4848
3
4949
}
50-
fn get_row_pin(&self, idx: usize) -> Self::InputPin {
50+
fn get_row_pin(&'a self, idx: usize) -> Self::InputPin {
5151
TestPin(idx as u32)
5252
}
53-
fn get_column_pin(&self, idx: usize) -> Self::OutputPin {
53+
fn get_column_pin(&'a self, idx: usize) -> Self::OutputPin {
5454
TestPin(idx as u32)
5555
}
5656
}

0 commit comments

Comments
 (0)