-
Notifications
You must be signed in to change notification settings - Fork 207
[wip] #[derive(Ereport)]
for serialization with known max lengths
#2246
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
base: master
Are you sure you want to change the base?
Conversation
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.
license-eye has totally checked 603 files.
Valid | Invalid | Ignored | Fixed |
---|---|---|---|
602 | 1 | 0 | 0 |
Click to see the invalid file list
- lib/ereport/examples/test-expanded.rs
@@ -0,0 +1,121 @@ | |||
#![feature(prelude_import)] |
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.
#![feature(prelude_import)] | |
// 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/. | |
#![feature(prelude_import)] |
Okay, so the current approach as of e2e5106 seems to be more or less functional. But, there's one big flaw: it turns out there isn't really a way to take something like the current trait: Lines 12 to 18 in 13de2cf
and write a function like pub fn encode_ereport<'buf, E: EreportData>(
ereport: &E,
buf: &'buf mut [u8; E::MAX_CBOR_LEN],
) -> &'buf [u8] {
let mut encoder = Encoder::new(buf);
todo!()
} because this runs afoul of
This feels pretty unfortunate, as a function like this is basically the UX I was hoping to be able to have. Currently, the user can get the type to tell it what the right length for the buffer is, and use it to construct a buffer, but we can't easily have an API that ensures you use the correct-length buffer when encoding, which is too bad. We could just enable the nightly-only Another option I'm thinking about is changing the trait to something like: pub trait EreportData: Encode<()> {
/// The maximum length of the CBOR-encoded representation of this value.
///
/// The value is free to encode fewer than this many bytes, but may not
/// encode more.
const MAX_CBOR_LEN: usize;
type NeededBuf: AsRef<[u8]> + AsMut<[u8]>;
} and having the derive macro generate a pub fn encode_ereport<'buf, E: EreportData>(
ereport: &E,
buf: &'buf mut E::NeededBuf,
) -> &'buf [u8] {
// ...
} and you wouldn't be able to call it with a too-short array. But, this might be overthinking it... |
Update: nope, the approach proposed in #2246 (comment) still ends up using a generic parameter in a const expression if the type is generic over another type implementing |
No description provided.