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

Feature request: implement From<Cow<'static, [u8]>> for Bytes #712

Open
alkarkhi opened this issue Jun 12, 2024 · 0 comments
Open

Feature request: implement From<Cow<'static, [u8]>> for Bytes #712

alkarkhi opened this issue Jun 12, 2024 · 0 comments

Comments

@alkarkhi
Copy link

alkarkhi commented Jun 12, 2024

I have a function that encodes an input Into<Cow<'static, [u8]>>. When there is encoding accepted I return a new Cow::Owned. But when there is no encoding accepted I just return the input. Here is what it looks like

pub struct Encoding {
    brotli: bool,
    gzip: bool,
}

impl Encoding {
    pub fn new(headers: &HeaderMap) -> Encoding {
        // init
    }

    pub fn encode<T>(&self, content: T) -> Cow<'static, [u8]>
    where 
        T: Into<Cow<'static, [u8]>>,
    {
        if self.brotli {
            // encode to brotli
        }
        else if self.gzip {
            // encode to gzip
        }
        else {
            content.into()
        }
    }
}

Then I have to convert this value into Bytes. For now I can either use match (which becomes too much boilerplate) or Cow::into_owned to convert to Vec<u8> and then Bytes (but that will convert any &'static [u8] to a Vec and thus allocate). I was wondering whether it would be possible to implement From<Cow<'static, [u8]>> for Bytes. From what I can see From<&'static [u8]> and From<Vec<u8>> are already implemented so I don't see an issue.

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

No branches or pull requests

1 participant