Skip to content

Instantly share code, notes, and snippets.

@brianmay
Created November 24, 2022 01:30
Show Gist options
  • Save brianmay/2c731c17f86c0ec5c2aa57d468de5217 to your computer and use it in GitHub Desktop.
Save brianmay/2c731c17f86c0ec5c2aa57d468de5217 to your computer and use it in GitHub Desktop.
Why does this not compile? If I inline the get_display function (see comments) it works
use esp_idf_hal::{
gpio::{InputPin, OutputPin},
i2c::{self, I2cDriver},
};
use shared_bus::{BusManager, I2cProxy, NullMutex};
use ssd1306::{
mode::BufferedGraphicsMode, prelude::I2CInterface, size::DisplaySize128x64, Ssd1306,
};
type SharedBus<'a> = BusManager<NullMutex<I2cDriver<'a>>>;
type Bus<'a> = I2cProxy<'a, NullMutex<I2cDriver<'a>>>;
type Display<'a> =
Ssd1306<I2CInterface<Bus<'a>>, DisplaySize128x64, BufferedGraphicsMode<DisplaySize128x64>>;
fn get_bus<SDA: InputPin + OutputPin, SCL: InputPin + OutputPin>(
_i2c: i2c::I2C0,
_scl: SCL,
_sda: SDA,
) -> Result<SharedBus<'static>, String> {
Err("oops".to_string())
}
fn get_display<'a>(_bus: Bus<'a>, _address: u8) -> Result<Display<'a>, String> {
Err("oops".to_string())
}
pub const NUM_DISPLAYS: usize = 2;
pub fn connect(
i2c: i2c::I2C0,
scl: impl InputPin + OutputPin + 'static,
sda: impl InputPin + OutputPin + 'static,
) {
let bus = get_bus(i2c, scl, sda).unwrap();
// let bus0 = bus.acquire_i2c();
// let display0 = {
// let di = ssd1306::I2CDisplayInterface::new_custom_address(bus0, 0x3c);
// let mut display = ssd1306::Ssd1306::new(
// di,
// ssd1306::size::DisplaySize128x64,
// ssd1306::rotation::DisplayRotation::Rotate0,
// )
// .into_buffered_graphics_mode();
// display
// };
// let bus1 = bus.acquire_i2c();
// let display1 = {
// let di = ssd1306::I2CDisplayInterface::new_custom_address(bus1, 0x3c);
// let mut display = ssd1306::Ssd1306::new(
// di,
// ssd1306::size::DisplaySize128x64,
// ssd1306::rotation::DisplayRotation::Rotate0,
// )
// .into_buffered_graphics_mode();
// display
// };
let display0 = get_display(bus.acquire_i2c(), 0x3C).unwrap();
let display1 = get_display(bus.acquire_i2c(), 0x3D).unwrap();
let mut displays: [_; NUM_DISPLAYS] = [display0, display1];
}
error[E0597]: `bus` does not live long enough
--> src/faulty.rs:66:32
|
66 | let display1 = get_display(bus.acquire_i2c(), 0x3D).unwrap();
| ^^^^^^^^^^^^^^^^^
| |
| borrowed value does not live long enough
| argument requires that `bus` is borrowed for `'static`
...
69 | }
| - `bus` dropped here while still borrowed
For more information about this error, try `rustc --explain E0597`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment