-
Notifications
You must be signed in to change notification settings - Fork 21
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
feat(api): make from_content_parts
accept Vec
#94
base: master
Are you sure you want to change the base?
Conversation
does it affect performance a lot? |
Using One alternative is to use a nested pub fn from_content_parts<S, T>(content_parts: &S) -> Self
where
T: AsRef<[u8]>,
S: AsRef<[T]>,
{ This requires a slight change at the call-site, using Example function calls: XorName::from_content_parts(&[b"abcdefg".as_slice(), b"hijk".as_slice()]); // as_slice here :(
XorName::from_content_parts(&vec![b"abcdefg".to_vec(), b"hijk".to_vec()]); What is the background of this PR, are you bumping into a problem somewere? |
My background is like in the code I linked to in OP, where complicated calls are being made to convert Vec to slices. My code (already uses proposed change): pub struct XorNameBuilder {
origin: XorName,
sources: Vec<Vec<u8>>,
}
impl XorNameBuilder {
pub fn from(xor_name: &XorName) -> Self {
Self {
origin: xor_name.clone(),
sources: vec![],
}
}
pub fn with_bytes(mut self, name: &[u8]) -> Self {
self.sources.push(name.to_vec());
self
}
pub fn with_str(mut self, name: &str) -> Self {
self.sources.push(name.as_bytes().to_vec());
self
}
pub fn build(self) -> XorName {
let mut built = self.origin.0;
if !self.sources.is_empty() {
let other = XorName::from_content_parts(self.sources);
for i in 0..xor_name::XOR_NAME_LEN {
built[i] = built[i] ^ other.0[i];
}
}
XorName(built)
}
} |
It seems it generates some errors. At the moment I don't have resources and Rust knowledge to dig deep enough into it by myself. Feel free to close the PR. error[E0277]: `&S` is not an iterator
--> src/lib.rs:127:21
|
127 | for part in content_parts {
| ^^^^^^^^^^^^^ `&S` is not an iterator
|
= help: the trait `Iterator` is not implemented for `&S`
= help: the trait `Iterator` is implemented for `&mut I`
= note: required for `&S` to implement `IntoIterator`
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
--> src/lib.rs:127:13
|
127 | for part in content_parts {
| ^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `[u8]`
= note: all local variables must have a statically known size
= help: unsized locals are gated as an unstable feature
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
--> src/lib.rs:127:21
|
127 | for part in content_parts {
| ^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `[u8]`
note: required by a bound in `std::option::Option`
--> /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/option.rs:570:1 |
it makes it easier to use, for example here:
https://github.com/maidsafe/safe_network_archive/blob/37f5b2391d5453f657cf840dd70efae4167502b3/sn_interface/src/network_knowledge/node_state/relocation.rs#L54