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

feat(api): make from_content_parts accept Vec #94

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

@loziniak loziniak requested a review from a team as a code owner March 15, 2024 21:41
@loziniak
Copy link
Author

does it affect performance a lot?

@b-zee
Copy link
Contributor

b-zee commented Mar 18, 2024

Using Vec can impact performance. Especially if at the call-site that Vec has to be created and cloned/passed into the method.

One alternative is to use a nested AsRef instead, so that it's more generic:

    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 &b"abc"[..] or b"abc".to_slice() instead of b"abc", although I'm unsure why.

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?

@loziniak
Copy link
Author

loziniak commented Mar 26, 2024

AsRef solution seems ok.

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)
    }
}

@loziniak
Copy link
Author

use a nested AsRef instead

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

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.

None yet

2 participants