Skip to content

Commit

Permalink
Add a lifetime parameter to MatrixConfig.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jgouly committed Oct 24, 2016
1 parent 64cdbe2 commit 9d31588
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
28 changes: 14 additions & 14 deletions src/matrix_config.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
pub struct RowPins<'a, MC: MatrixConfig + 'a> {
pub struct RowPins<'a, MC: MatrixConfig<'a> + 'a> {
count: usize,
conf: &'a MC,
}

impl<'a, MC: MatrixConfig + 'a> RowPins<'a, MC> {
impl<'a, MC: MatrixConfig<'a>> RowPins<'a, MC> {
fn new(conf: &'a MC) -> RowPins<'a, MC> {
RowPins {
count: 0,
Expand All @@ -12,7 +12,7 @@ impl<'a, MC: MatrixConfig + 'a> RowPins<'a, MC> {
}
}

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

pub struct ColumnPins<'a, MC: MatrixConfig + 'a> {
pub struct ColumnPins<'a, MC: MatrixConfig<'a> + 'a> {
count: usize,
conf: &'a MC,
}

impl<'a, MC: MatrixConfig + 'a> ColumnPins<'a, MC> {
impl<'a, MC: MatrixConfig<'a>> ColumnPins<'a, MC> {
fn new(conf: &'a MC) -> ColumnPins<'a, MC> {
ColumnPins {
count: 0,
Expand All @@ -39,7 +39,7 @@ impl<'a, MC: MatrixConfig + 'a> ColumnPins<'a, MC> {
}
}

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

pub trait MatrixConfig {
pub trait MatrixConfig<'a> {
type InputPin;
type OutputPin;

fn get_num_rows(&self) -> usize;
fn get_num_columns(&self) -> usize;

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

fn rows(&self) -> RowPins<Self>
fn rows(&'a self) -> RowPins<Self>
where Self: Sized
{
RowPins::new(&self)
}
fn columns(&self) -> ColumnPins<Self>
fn columns(&'a self) -> ColumnPins<Self>
where Self: Sized
{
ColumnPins::new(&self)
Expand All @@ -80,7 +80,7 @@ mod tests {
#[test]
fn basic() {
struct TestMatrixConfig {}
impl MatrixConfig for TestMatrixConfig {
impl<'a> MatrixConfig<'a> for TestMatrixConfig {
type InputPin = u32;
type OutputPin = u32;
fn get_num_rows(&self) -> usize {
Expand All @@ -89,10 +89,10 @@ mod tests {
fn get_num_columns(&self) -> usize {
3
}
fn get_row_pin(&self, idx: usize) -> Self::InputPin {
fn get_row_pin(&'a self, idx: usize) -> Self::InputPin {
idx as u32
}
fn get_column_pin(&self, idx: usize) -> Self::OutputPin {
fn get_column_pin(&'a self, idx: usize) -> Self::OutputPin {
idx as u32
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub trait OutputPin {
fn set_high(&self);
}

pub fn single_scan<MC: MatrixConfig, RM: Matrix<u32>>(conf: &MC) -> RM
pub fn single_scan<'a, MC: MatrixConfig<'a>, RM: Matrix<u32>>(conf: &'a MC) -> RM
where MC::InputPin: InputPin,
MC::OutputPin: OutputPin
{
Expand Down Expand Up @@ -38,7 +38,7 @@ pub fn private_basic() {
fn set_low(&self) {}
fn set_high(&self) {}
}
impl MatrixConfig for TestMatrixConfig {
impl<'a> MatrixConfig<'a> for TestMatrixConfig {
type InputPin = TestPin;
type OutputPin = TestPin;
fn get_num_rows(&self) -> usize {
Expand All @@ -47,10 +47,10 @@ pub fn private_basic() {
fn get_num_columns(&self) -> usize {
3
}
fn get_row_pin(&self, idx: usize) -> Self::InputPin {
fn get_row_pin(&'a self, idx: usize) -> Self::InputPin {
TestPin(idx as u32)
}
fn get_column_pin(&self, idx: usize) -> Self::OutputPin {
fn get_column_pin(&'a self, idx: usize) -> Self::OutputPin {
TestPin(idx as u32)
}
}
Expand Down

0 comments on commit 9d31588

Please sign in to comment.