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

Add deconstructing conversions for owned Elements #890

Merged
merged 2 commits into from
Jan 3, 2025

Conversation

jpschorr
Copy link
Contributor

Allows deconstruction owned Elements and Bytes into their contained data.

See also previous enhancements to ergonomics of owned Elements
Cf. #858
Cf. #866


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

Copy link

codecov bot commented Dec 31, 2024

Codecov Report

Attention: Patch coverage is 91.66667% with 2 lines in your changes missing coverage. Please review.

Project coverage is 77.86%. Comparing base (75b0763) to head (ed56ad0).

Files with missing lines Patch % Lines
src/element/mod.rs 90.47% 2 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main     #890   +/-   ##
=======================================
  Coverage   77.85%   77.86%           
=======================================
  Files         136      136           
  Lines       34703    34727   +24     
  Branches    34703    34727   +24     
=======================================
+ Hits        27017    27039   +22     
- Misses       5683     5685    +2     
  Partials     2003     2003           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@popematt popematt left a comment

Choose a reason for hiding this comment

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

Overall, looks good. I could see some of these maybe being better served by a From implementation, such as into_value and into_bytes. What do you think?

Comment on lines 28 to 29
pub fn into_bytes(self) -> Vec<u8> {
self.data
}
Copy link
Contributor

Choose a reason for hiding this comment

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

What are your thoughts on into_bytes() vs impl From<Bytes> for Vec<u8>? It might be more ergonomic because then Bytes could be passed to a function that accepts Into<Vec<u8>> . On the other hand, if we think we might ever have a lazy Bytes, then maybe it's best to leave it the way it is. @zslayton what do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

impl From<Element> for Value conflicts with the already present Value to Element From implementation.

// Anything that can be turned into a `Value` can then be turned into an `Element`
// by associating it with an empty annotations sequence.
impl<T> From<T> for Element
where
    T: Into<Value>,
{
    fn from(value: T) -> Self {
        Element::new(Annotations::empty(), value.into())
    }
}

impl From<Element> for Value {
    fn from(element: Element) -> Self {
        element.value
    }
}
error[E0119]: conflicting implementations of trait `From<element::Element>` for type `element::Element`
   --> src/element/mod.rs:973:1
    |
973 | / impl<T> From<T> for Element
974 | | where
975 | |     T: Into<Value>,
    | |___________________^
    |
    = note: conflicting implementation in crate `core`:
            - impl<T> From<T> for T;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have changed the into_bytes into a From<Bytes> for Vec<u8>, though I could change it back if desired (or have both). It was inspired by https://doc.rust-lang.org/std/string/struct.String.html#method.into_bytes

@jpschorr jpschorr force-pushed the feat-decon-owned-elt branch from c398db2 to ed56ad0 Compare January 3, 2025 18:54
@zslayton zslayton merged commit 46cc6b2 into main Jan 3, 2025
30 of 31 checks passed
@zslayton zslayton deleted the feat-decon-owned-elt branch January 3, 2025 21:02
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.

3 participants