Hi,
after a while learning and reading about Rust, I decided that it's now time for me to contribute to Embedded Rust with a crate of my own
Unfortunately I hit a roadblock with the type system.
I want to create a struct that holds the pins for a 8-bit parallel data bus:
pub struct DataBus<T> {
pins: [T; 8],
}
impl<T> DataBus<T>
where T: embedded_hal::digital::v2::OutputPin {
pub fn new(pins: [T; 8]) -> DataBus<T> {
DataBus {
pins
}
}
}
since every pin has its own type, I made the array generic over T
.
Now, when I want to instantiate a DataBus
like this:
let d0 = gpiob.pb0.into_open_drain_output(&mut gpiob.crl);
let d1 = gpiob.pb1.into_open_drain_output(&mut gpiob.crl);
let d2 = gpiob.pb2.into_open_drain_output(&mut gpiob.crl);
let d3 = gpiob.pb5.into_open_drain_output(&mut gpiob.crl);
let d4 = gpiob.pb6.into_open_drain_output(&mut gpiob.crl);
let d5 = gpiob.pb7.into_open_drain_output(&mut gpiob.crl);
let d6 = gpiob.pb8.into_open_drain_output(&mut gpiob.crh);
let d7 = gpiob.pb9.into_open_drain_output(&mut gpiob.crh);
let dbus: [embedded_hal::digital::v2::OutputPin; 8] = [d0, d1, d2, d3, d4, d5, d6, d7];
let data_bus = DataBus::new(dbus);
I get the following error:
error[E0277]: the size for values of type `dyn embedded_hal::digital::v2::OutputPin` cannot be known at compilation time
--> src/main.rs:64:20
|
64 | let data_bus = DataBus::new(dbus);
| ^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
::: src/ili9486/gpio.rs:61:20
|
61 | pub struct DataBus<T> {
| - required by this bound in `ili9486::gpio::DataBus`
|
= help: the trait `core::marker::Sized` is not implemented for `dyn embedded_hal::digital::v2::OutputPin`
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
I did read the referenced chapter in the book, but I'm still lost with this. How can I create an array of pins? I would not like to have a struct like this:
pub struct DataBus<D0, D1, D2, D3, D4, D5, D6, D7> {
d0: D0,
d1: D1,
d2: D2,
d3: D3,
d4: D4,
d5: D5,
d6: D6,
d7: D7,
}
impl<D0, D1, D2, D3, D4, D5, D6, D7> DataBus<D0, D1, D2, D3, D4, D5, D6, D7>
where
D0: embedded_hal::digital::v2::OutputPin,
D1: embedded_hal::digital::v2::OutputPin,
D2: embedded_hal::digital::v2::OutputPin,
D3: embedded_hal::digital::v2::OutputPin,
D4: embedded_hal::digital::v2::OutputPin,
D5: embedded_hal::digital::v2::OutputPin,
D6: embedded_hal::digital::v2::OutputPin,
D7: embedded_hal::digital::v2::OutputPin,
{
pub fn new(
d0: D0,
d1: D1,
d2: D2,
d3: D3,
d4: D4,
d5: D5,
d6: D6,
d7: D7,
) -> DataBus<D0, D1, D2, D3, D4, D5, D6, D7> {
DataBus {
d0,
d1,
d2,
d3,
d4,
d5,
d6,
d7,
}
}
}