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

Add support for multiple heapless and heapless-bytes versions #13

Merged
merged 5 commits into from
Oct 9, 2024

Conversation

sosthene-nitrokey
Copy link
Contributor

Copy link
Member

@robin-nitrokey robin-nitrokey left a comment

Choose a reason for hiding this comment

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

We are now adding these helper traits to multiple crates (e. g. littlefs2) and will probably continue to do so. Maybe it would make sense to move them to a separate crate.

}

#[cfg(feature = "heapless-bytes-v0-3")]
/// Append serialization of object to existing bytes, returning length of serialized object.
pub fn cbor_serialize_extending_bytes<T: ?Sized + serde::Serialize, const N: usize>(
Copy link
Member

Choose a reason for hiding this comment

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

Should we deprecate this function in favor of cbor_serialize_to? It would mean that we could need additional type annotations in some cases, but it is somewhat inconsistent to only provide this function for heapless-bytes 0.3.

We don’t need to do it in this PR but should at least create an issue for it if we don’t.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It was left for backwards compatibility for Nitrokey/nitrokey-3-firmware#538, otherwise I would have removed it yes.

Copy link
Member

Choose a reason for hiding this comment

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

I was just thinking of adding the #[deprecated] attribute. We can then remove it later.

Copy link
Contributor Author

@sosthene-nitrokey sosthene-nitrokey Oct 9, 2024

Choose a reason for hiding this comment

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

I think I would:

  1. merge this as is (so we don't get warnings in our current releases)
  2. remove it for the 0.5.0 release.

Copy link
Member

Choose a reason for hiding this comment

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

If you want a compatible intermediate state, we should also add heapless-bytes-v0-3 to the default features (and remove it for the release).


object.serialize(&mut ser)?;

Ok(ser.into_inner().len() - len_before)
}

#[cfg(feature = "heapless-bytes-v0-3")]
/// Serialize object into newly allocated Bytes.
pub fn cbor_serialize_bytes<T: ?Sized + serde::Serialize, const N: usize>(
Copy link
Member

Choose a reason for hiding this comment

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

Same as above – a replacement could use Writer + Default.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same as above, kept for backwards compatibility. But maybe we tag this as a nitrokey release, and merge it removing the old function (maybe in a separate PR, that cleans everything up for 0.5.0).

src/ser.rs Outdated
Comment on lines 82 to 86
// #[derive(Debug)]
// pub struct SliceWriter<'a> {
// slice: &'a mut [u8],
// index: usize,
// }
Copy link
Member

Choose a reason for hiding this comment

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

Why do you want to keep this?

Comment on lines +138 to +139
self.written += buf.len();
self.writer.write_all(buf)
Copy link
Member

Choose a reason for hiding this comment

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

I think written should only be increased if write_all is successful. I don’t think this is the case today (or maybe ever) but theoretically, serialization could continue after a failure.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If we follow the write_all from the standard library we could also have a write_all failure that has written some bytes. This is also internal API, and can be adapted in the future if required.

If we want to think forward, we should probably have a only a write function, and have write_all be a default impl.

Copy link
Member

Choose a reason for hiding this comment

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

While the API is internal, the behavior of write_all is part of the public API via Serializer.

@sosthene-nitrokey
Copy link
Contributor Author

We are now adding these helper traits to multiple crates (e. g. littlefs2) and will probably continue to do so. Maybe it would make sense to move them to a separate crate.

We could also just use core_io instead.

@robin-nitrokey
Copy link
Member

I think it would not work for littlefs2, at least not without additional overhead, because we need direct access to the buffer so that we can pass it to the C API. But it could be an option for this crate. It would be hard to support the old heapless and heapless-bytes versions though.

@robin-nitrokey
Copy link
Member

embedded_io::Write would probably be a better choice as it is maintained by the Rust Embedded WG. But it would be great to have a 1.0 release before adopting it.

@sosthene-nitrokey
Copy link
Contributor Author

sosthene-nitrokey commented Oct 8, 2024

Yeah, I just found it too. For littlefs the optimal solution would be to use BufMut from bytes, but there is an open issue to have it in heapless: rust-embedded/heapless#340 that hasn't moved, we could have some #[repr(transparent)] wrapper for that in heapless-bytes

This allows better backwards compatibility and serializing to heapless directly.

Given #11 I went with a breaking change update
(the `Serializer` field was public and existing implementations are put behind a breaking change)
Copy link
Member

@robin-nitrokey robin-nitrokey left a comment

Choose a reason for hiding this comment

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

I’d remove the dead code, otherwise LGTM.

@sosthene-nitrokey sosthene-nitrokey merged commit 4a36846 into main Oct 9, 2024
1 check passed
@sosthene-nitrokey sosthene-nitrokey deleted the serialize-heapless branch October 9, 2024 14:06
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.

2 participants