Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: Relax lifetime constraint on msgs in I2CTransfer::transfer() #89

Merged
merged 2 commits into from
Dec 20, 2024

Conversation

peterdelevoryas
Copy link
Contributor

This change enables users of i2cdev to create generic functions on T: I2CTransfer that have output buffers constructed separately from the I2CMessage array, like the following:

fn smbus_read_post_box<T>(i2c: &mut T, offset: u16, out: &mut [u8])
	where for<'a> T: I2CTransfer<'a>,
{
	let addr = offset.to_be_bytes();
	let mut messages = [
		T::Message::write(addr),
		T::Message::read(out),
	];
	i2c.transfer(&mut messages).expect("uh oh");
}

Before this, messages would not satisfy the constraints of .transfer(), because messages does not live as long as one of the output buffers out:

error[E0597]: `messages` does not live long enough
  --> src/smbpbisensor.rs:69:19
   |
63 |     let mut messages = [
   |         ------------ binding `messages` declared here
...
69 |         .transfer(&mut messages)
   |                   ^^^^^^^^^^^^^ borrowed value does not live long enough
...
78 | }
   | -
   | |
   | `messages` dropped here while still borrowed
   | borrow might be used here, when `messages` is dropped and runs the destructor for type `[<T as I2CTransfer<'_>>::Message; 2]`

The error message is a little confusing, but basically &'a mut [Self::Message] is forcing the array of I2CMessages to match the lifetime of the buffers in the messages, which is not strictly necessary: the array of messages can have a different lifetime than the buffers. After this change, the above example compiles successfully.

@peterdelevoryas peterdelevoryas requested a review from a team as a code owner December 4, 2024 22:13
Copy link
Member

@eldruin eldruin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Sounds good to me, thank you!
Could you add an entry to the changelog?

@peterdelevoryas
Copy link
Contributor Author

Good point. Sounds good to me, thank you! Could you add an entry to the changelog?

Yes absolutely! Sorry, got caught up with other stuff today. Revised it just now.

Copy link
Member

@eldruin eldruin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you.
Could you move your changelog entry to the unreleased section?

@peterdelevoryas
Copy link
Contributor Author

Thank you. Could you move your changelog entry to the unreleased section?

Oh argh, I misread the changelog section headers yeah I’ll do that

@peterdelevoryas
Copy link
Contributor Author

Ok, changelog is now in the appropriate section

@peterdelevoryas
Copy link
Contributor Author

CI only failed because no macOS runners are available? mebe this is ok to merge? Sorry for requiring 2 updates for the changelog :(

@peterdelevoryas
Copy link
Contributor Author

Hey @eldruin, think we could merge this now?

eldruin
eldruin previously approved these changes Dec 20, 2024
Copy link
Member

@eldruin eldruin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks good to me, thank you. Let's see if a macOS runner picks the CI job up.

@eldruin eldruin enabled auto-merge December 20, 2024 12:00
@kevinmehall
Copy link
Contributor

The issue may be that it's trying to use macos-11, which was removed in June.

@eldruin
Copy link
Member

eldruin commented Dec 20, 2024

Thanks for the hint @kevinmehall. @peterdelevoryas could you change the platform to macos-14 in the ci.yml?

@peterdelevoryas
Copy link
Contributor Author

Thanks for the hint @kevinmehall. @peterdelevoryas could you change the platform to macos-14 in the ci.yml?

Absolutely! I was wondering if maybe you guys would be ok with me changing that in this diff

macOS 11 runners have been deprecated! Need this for CI to pass.
This change enables users of `i2cdev` to create generic functions on `T:
I2CTransfer` that have output buffers constructed separately from the
`I2CMessage` array, like the following:

```rust
fn smbus_read_post_box<T>(i2c: &mut T, offset: u16, out: &mut [u8])
	where for<'a> T: I2CTransfer<'a>,
{
	let addr = offset.to_be_bytes();
	let mut messages = [
		T::Message::write(addr),
		T::Message::read(out),
	];
	i2c.transfer(&mut messages).expect("uh oh");
}
```

Before this, `messages` would not satisfy the constraints of `.transfer()`,
because `messages` does not live as long as one of the output buffers `out`:

```
error[E0597]: `messages` does not live long enough
  --> src/smbpbisensor.rs:69:19
   |
63 |     let mut messages = [
   |         ------------ binding `messages` declared here
...
69 |         .transfer(&mut messages)
   |                   ^^^^^^^^^^^^^ borrowed value does not live long enough
...
78 | }
   | -
   | |
   | `messages` dropped here while still borrowed
   | borrow might be used here, when `messages` is dropped and runs the destructor for type `[<T as I2CTransfer<'_>>::Message; 2]`
```

The error message is a little confusing, but basically `&'a mut [Self::Message]`
is forcing the array of `I2CMessage`s to match the lifetime of the buffers in
the messages, which is not strictly necessary: the array of messages can have a
different lifetime than the buffers. After this change, the above example
compiles successfully.
auto-merge was automatically disabled December 20, 2024 21:30

Head branch was pushed to by a user without write access

@peterdelevoryas
Copy link
Contributor Author

Yay! All the CI passed, I think we're finally ready for merging? @eldruin

Copy link
Member

@eldruin eldruin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thank you so much!

@eldruin eldruin added this pull request to the merge queue Dec 20, 2024
Merged via the queue into rust-embedded:master with commit 69121a8 Dec 20, 2024
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants