Skip to content

Commit 70965e5

Browse files
committed
Enforce data lifetimes
1 parent f165681 commit 70965e5

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

src/core.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,21 +123,21 @@ pub trait I2CDevice {
123123
///
124124
/// Typical implementations will store state with references to the bus
125125
/// in use. The trait is based on the Linux i2cdev interface.
126-
pub trait I2CBus {
126+
pub trait I2CBus<'a> {
127127
type Error: Error;
128-
type Message: I2CMessage;
128+
type Message: I2CMessage<'a>;
129129

130130
// Performs multiple serially chained I2C read/write transactions. On
131131
// success the return code is the number of successfully executed
132132
// transactions
133-
fn transfer(&mut self, msgs: &mut [Self::Message]) -> Result<u32, Self::Error>;
133+
fn transfer(&mut self, msgs: &'a mut [Self::Message]) -> Result<u32, Self::Error>;
134134
}
135135

136136
/// Read/Write I2C message
137-
pub trait I2CMessage {
137+
pub trait I2CMessage<'a> {
138138
/// Read data from device
139-
fn read(slave_address: u16, data: &mut [u8]) -> Self;
139+
fn read(slave_address: u16, data: &'a mut [u8]) -> Self;
140140

141141
/// Write data to device
142-
fn write(slave_address: u16, data: &[u8]) -> Self;
142+
fn write(slave_address: u16, data: &'a [u8]) -> Self;
143143
}

src/linux.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -251,14 +251,14 @@ impl LinuxI2CBus {
251251
}
252252

253253
/// Linux I2C message
254-
pub type LinuxI2CMessage = ffi::i2c_msg;
254+
pub type LinuxI2CMessage<'a> = ffi::i2c_msg;
255255

256-
impl I2CBus for LinuxI2CBus {
256+
impl<'a> I2CBus<'a> for LinuxI2CBus {
257257
type Error = LinuxI2CError;
258-
type Message = LinuxI2CMessage;
258+
type Message = LinuxI2CMessage<'a>;
259259

260260
/// Issue the provided sequence of I2C transactions
261-
fn transfer(&mut self, msgs: &mut [Self::Message]) -> Result<u32, LinuxI2CError> {
261+
fn transfer(&mut self, msgs: &'a mut [Self::Message]) -> Result<u32, LinuxI2CError> {
262262
ffi::i2c_rdwr(self.as_raw_fd(), msgs).map_err(From::from)
263263
}
264264
}
@@ -288,8 +288,8 @@ bitflags! {
288288
}
289289
}
290290

291-
impl I2CMessage for LinuxI2CMessage {
292-
fn read(slave_address: u16, data: &mut [u8]) -> LinuxI2CMessage {
291+
impl<'a> I2CMessage<'a> for LinuxI2CMessage<'a> {
292+
fn read(slave_address: u16, data: &'a mut [u8]) -> LinuxI2CMessage {
293293
Self {
294294
addr: slave_address,
295295
flags: I2CMessageFlags::READ.bits(),
@@ -298,7 +298,7 @@ impl I2CMessage for LinuxI2CMessage {
298298
}
299299
}
300300

301-
fn write(slave_address: u16, data: &[u8]) -> LinuxI2CMessage {
301+
fn write(slave_address: u16, data: &'a [u8]) -> LinuxI2CMessage {
302302
Self {
303303
addr: slave_address,
304304
flags: I2CMessageFlags::empty().bits(),
@@ -308,7 +308,7 @@ impl I2CMessage for LinuxI2CMessage {
308308
}
309309
}
310310

311-
impl LinuxI2CMessage {
311+
impl<'a> LinuxI2CMessage<'a> {
312312
pub fn with_flags(self, flags: I2CMessageFlags) -> Self {
313313
Self {
314314
addr: self.addr,

0 commit comments

Comments
 (0)