-
Notifications
You must be signed in to change notification settings - Fork 208
minicbor-lease adapter for encoding straight into a lease
#2164
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
Merged
+217
−112
Merged
Changes from 12 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
399a95f
start minicbor lease adapter thingy
hawkw eb3bed8
wip
hawkw a396724
okay now it works
hawkw 3d02869
oh clippy dislikes that, okay
hawkw 8df897c
...i'm dumb
hawkw 53e052b
@mkeeter suggestions
hawkw 70e0623
additional @mkeeter suggestions
hawkw 2ad5c71
Update to `minicbor` v2, remove error nonsense
hawkw 6dff3ca
minicbor to 2.1.1
hawkw 2c72a05
attempt to fix ssmarshal
hawkw 8af42e9
stop trying to build x86_64 tests
hawkw 0bef2ac
Merge branch 'master' into eliza/minicbor-lease
hawkw 077fe43
@mkeeter review feedback
hawkw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| [package] | ||
| name = "minicbor-lease" | ||
| version = "0.1.0" | ||
| edition = "2024" | ||
|
|
||
| [dependencies] | ||
| minicbor = { workspace = true } | ||
| idol-runtime = { workspace = true } | ||
|
|
||
| [lib] | ||
| test = false | ||
| doctest = false | ||
| bench = false | ||
|
|
||
| [lints] | ||
| workspace = true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| // This Source Code Form is subject to the terms of the Mozilla Public | ||
| // License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| // file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
|
||
| //! An adapter implementing [`minicbor::encode::write::Write`] for | ||
| //! [`idol_runtime::Leased`] byte buffers. | ||
| #![no_std] | ||
|
|
||
| /// An adapter implementing [`minicbor::encode::write::Write`] for | ||
| /// [`idol_runtime::Leased`] byte buffers. | ||
| pub struct LeasedWriter<'lease, A> | ||
| where | ||
| A: idol_runtime::AttributeWrite, | ||
| { | ||
| lease: &'lease mut idol_runtime::Leased<A, [u8]>, | ||
| pos: usize, | ||
| ran_out_of_space: bool, | ||
hawkw marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /// Errors returned by the [`minicbor::encode::write::Write`] implementation for | ||
| /// [`LeasedWriter`]. | ||
| #[derive(Copy, Clone, PartialEq, Debug)] | ||
| pub enum Error { | ||
| /// The other side of the lease has gone away. | ||
| WentAway, | ||
| /// Data could not be written as there was no room left in the lease. | ||
| EndOfLease, | ||
| } | ||
|
|
||
| impl core::fmt::Display for Error { | ||
| fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { | ||
| f.write_str(match self { | ||
| Self::WentAway => "lease went away", | ||
| Self::EndOfLease => "end of lease", | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| impl<A> minicbor::encode::write::Write for LeasedWriter<'_, A> | ||
| where | ||
| A: idol_runtime::AttributeWrite, | ||
| { | ||
| type Error = Error; | ||
|
|
||
| fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error> { | ||
| let Some(end) = self.pos.checked_add(buf.len()) else { | ||
| self.ran_out_of_space = true; | ||
| return Err(Error::EndOfLease); | ||
| }; | ||
| if end >= self.lease.len() { | ||
| self.ran_out_of_space = true; | ||
| return Err(Error::EndOfLease); | ||
| } | ||
| self.lease | ||
| .write_range(self.pos..end, buf) | ||
| .map_err(|_| Error::WentAway)?; | ||
|
|
||
| self.pos += buf.len(); | ||
|
|
||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| impl<'lease, A> LeasedWriter<'lease, A> | ||
| where | ||
| A: idol_runtime::AttributeWrite, | ||
| { | ||
| /// Returns a new `LeasedWriter` starting at byte 0 of the lease. | ||
| pub fn new(lease: &'lease mut idol_runtime::Leased<A, [u8]>) -> Self { | ||
| Self { | ||
| lease, | ||
| pos: 0, | ||
| ran_out_of_space: false, | ||
| } | ||
| } | ||
|
|
||
| /// Returns a new `LeasedWriter` starting at the specified byte position in | ||
| /// the lease. | ||
| /// | ||
| /// This is intended for cases where some data has already been written to | ||
| /// the lease. | ||
| pub fn starting_at( | ||
| position: usize, | ||
| lease: &'lease mut idol_runtime::Leased<A, [u8]>, | ||
| ) -> Self { | ||
| Self { | ||
| lease, | ||
| pos: position, | ||
| ran_out_of_space: false, | ||
| } | ||
| } | ||
|
|
||
| /// Returns the current byte position within the lease. | ||
| pub fn position(&self) -> usize { | ||
| self.pos | ||
| } | ||
|
|
||
| /// Borrows the underlying lease from the writer. | ||
| pub fn lease(&self) -> &idol_runtime::Leased<A, [u8]> { | ||
| self.lease | ||
| } | ||
|
|
||
| /// Returns the underlying lease, consuming the writer. | ||
| pub fn into_inner(self) -> &'lease mut idol_runtime::Leased<A, [u8]> { | ||
| self.lease | ||
| } | ||
|
|
||
| /// Returns `true` if the last `w | ||
| pub fn ran_out_of_space(&self) -> bool { | ||
| self.ran_out_of_space | ||
| } | ||
| } | ||
|
|
||
| impl From<Error> for idol_runtime::ClientError { | ||
| fn from(error: Error) -> Self { | ||
| match error { | ||
| Error::EndOfLease => idol_runtime::ClientError::BadLease, | ||
| Error::WentAway => idol_runtime::ClientError::WentAway, | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is necessary due to
ssmarshalno longer compiling with Rust 2024, as when the edition is Rust 2024,serdewill requirecore::error::Errorimplementations for error types, andssmarshaldoesn't have those.Since the last commit to ssmarshal's
mainbranch was 8 years ago, I'm not convinced this PR gets merged any time soon. It would probably be worth getting rid of our ssmarshal deps at some point, since Hubpack is basically the same thing but more featureful...