-
Notifications
You must be signed in to change notification settings - Fork 9
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
From slice take and split #11
Comments
When I was checking the serialisation module of plonk I was thinking something similar, but I'm still unsure it's a good API. First, it can be easily obtain using |
One of the most used bytes crate uses this pattern as well |
When considering API I usually prefer to the idioms used in the Rust core languages as first, so if you can think of anything similar there that we can take a look let me know; I will check the |
I pursued the idea of a Instead of having something like: // This is our common scenario, where we use `from_bytes`
// from inside another `from_bytes`
fn from_bytes(bytes: &[u8; Self::SIZE]) -> Result<Self, Self::Error> {
let a = BlsScalar::from_slice(&bytes[..32])?;
let b = BlsScalar::from_slice(&bytes[32..64])?;
let c = BlsScalar::from_slice(&bytes[64..])?;
Ok(Self { a, b, c })
} We can have: fn from_bytes(bytes: &[u8; Self::SIZE]) -> Result<Self, Self::Error> {
let mut bytes = &bytes[..];
let a = BlsScalar::from_slice_mut(&mut bytes)?;
let b = BlsScalar::from_slice_mut(&mut bytes)?;
let c = BlsScalar::from_slice_mut(&mut bytes)?;
Ok(Self { a, b, c })
} It also take in account if the buffer is not enough or if the bytes are not valid to be used to build the type. In the first case the bytes ARE NOT consumed (since there aren't enough bytes) and the error is returned; in the second case the bytes ARE consumed (since there are enough) but once we try to use them to build the type we got the error back. The bonus point of introducing a method like this one, is that we do not make assumption of the SIZE of the type we're reading. In fact, instead of using |
I would go go that! It looks really nice! |
As I mentioned in the rocket chat, the name is misleading (I used the one I initially proposed) since a) it does not return a mutable (the naming convention for the suffix It's just happen that we only have fn from_bytes(bytes: &[u8; Self::SIZE]) -> Result<Self, Self::Error> {
let mut bytes = &bytes[..];
let a = BlsScalar::from_reader(&mut bytes)?;
let b = BlsScalar::from_reader(&mut bytes)?;
let c = BlsScalar::from_reader(&mut bytes)?;
Ok(Self { a, b, c })
} Currently it's added to |
- Add `serialize::Read` trait Resolves: #11
A recurrent pattern is when we have multiple serial structs in a single slice of bytes.
One efficient solution is to have a function
fn from_slice_split<T>(bytes: &[u8]) -> Result<(T, &[u8]), _>
It will take the first
n
bytes required to parse the provided type, split the slice and return a tuple with the parsed type and the remainder bytes.Considering the required traits are implemented for T and we have a tuple
(T, T) = (a, b)
, the following test case should pass:The text was updated successfully, but these errors were encountered: