-
Notifications
You must be signed in to change notification settings - Fork 41
Expand impl Encode for Vec<String>
to impl Encode for Vec<E: Encode>
#314
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
Comments
@Eugeny can you open a PR? |
I would but my PRs from November are still hanging open :( |
I will get to them soon, I am just going through a very, very large backlog |
I had a go at implementing this, adding a very slightly modified version of the #[cfg(feature = "alloc")]
impl<E: Encode> Encode for Vec<E> {
fn encoded_len(&self) -> Result<usize, Error> {
self.iter().try_fold(4usize, |acc, e| {
acc.checked_add(e.encoded_len()?).ok_or(Error::Length)
})
}
fn encode(&self, writer: &mut impl Writer) -> Result<(), Error> {
self.encoded_len()?
.checked_sub(4)
.ok_or(Error::Length)?
.encode(writer)?;
for entry in self {
entry.encode(writer)?;
}
Ok(())
}
} Seems straight forward. However, this collides with the use criterion::{Criterion, criterion_group, criterion_main};
use ssh_encoding::Encode;
use std::hint::black_box;
fn encode_to_vec(bytes: &Vec<u8>, vec: &mut Vec<u8>) {
bytes.encode(vec).unwrap();
}
fn criterion_benchmark(c: &mut Criterion) {
let mut buf = Vec::new();
let bytes = vec![0u8; 1024];
c.bench_function("encode byte slice", |b| {
b.iter(|| encode_to_vec(black_box(&bytes), black_box(&mut buf)));
});
} Running this benchmark before and after applying the patch results in a very significant difference.
Unless the generic implementation can be made much smarter than what I did, I don't think it would be sensible to make this change. Perhaps the correct thing to do is instead to add more impls, and/or slightly less generic ones that don't collide with the basic ones that are already there. |
I think some marker trait which denotes that encoding a message this way is actually valid for a given type would be helpful, and may even get rid of the impl conflicts |
This is in turn used to implement `Encode` for slices of string-like types. Downstream users could implement the marker trait for their own types and get the same behavior for slices of their own types. Closes RustCrypto#314.
Uh oh!
There was an error while loading. Please reload this page.
Would be useful for
Vec<&str>
and key exchangeVec<Algorithm>
lists for example.The text was updated successfully, but these errors were encountered: