Skip to content

Add Bytes DataGuard #2362

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

Closed
wants to merge 1 commit into from
Closed

Add Bytes DataGuard #2362

wants to merge 1 commit into from

Conversation

chinoto
Copy link

@chinoto chinoto commented Oct 8, 2022

This will hopefully be sufficient for most of those concerned with issue #2148.
I might have gone the route of #2361 (implementing for &[u8]), but some people may want to get the filename and content-type as well, though I think that PR is still nice to have if all you need is the data.

content_type: Option<ContentType>,
) -> io::Result<Capped<Bytes<'a>>> {
let limit = { content_type.as_ref() }
.and_then(|ct| ct.extension()?.limits().find(&["file", ext.as_str()]))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there's a mistake here, shouldn't be

            .and_then(|ct| ct.extension())
            .and_then(|ext| req.limits().find(&["file", ext.as_str()]))

?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay, been busy working.
There is no need for a second .and_then(...) because the ? after .extension() extracts the value from Option::Some(T) or returns Option::None depending on which variant it happened to be, allowing for more concise code.
Apparently it was stabilized back in 2016, feels newer. There's a section in the book about it.

pub struct Bytes<'v> {
pub file_name: Option<&'v FileName>,
pub content_type: Option<ContentType>,
pub content: Cow<'v, [u8]>,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wondering, is there a reason for using Cow<[u8]> here instead of Vec<u8>? As far as I can tell converting a Cow<[T]> back into a Vec<T> would cause a reallocation of the entire file even if it's owned and I would rather have the Vec for my use-case.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm finally done with having to work so much, so hopefully I'll be more responsive now.
On line 52, I use it for the borrowed variant, so there is a purpose in using it and there should not be an unnecessary (re)allocation because:

Calling into_owned on a Cow::Owned returns the owned data. The data is moved out of the Cow without being cloned.

For extra clarification, the type of content doesn't mention Vec because it is inferred from the ToOwned implementation for slices that specifies its Owned variant is Vec.

Copy link
Member

@SergioBenitez SergioBenitez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implementation should just be delegating to the existing &[u8] and Vec<u8> FromData implementations. See 569bc09 for how this was done for the &[u8] FromForm implementation.

@SergioBenitez
Copy link
Member

Closing due to inactivity (apologies as I know it's largely on me), though I'm entirely open to this PR with the requested changed above.

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.

4 participants