Add impl AsRef<[u8]> for Uuid #472

Closed
opened 2020-05-23 20:41:08 -05:00 by lassade · 4 comments
lassade commented 2020-05-23 20:41:08 -05:00 (Migrated from github.com)

Is your feature request related to a problem? Please describe.
No

Describe the solution you'd like
Add some along this lines:

impl AsRef<[u8]> for Uuid {
    #[inline]
    fn as_ref(&self) -> &[u8] {
        &self.as_bytes()[..]
    }
}

Is it blocking?
No

Describe alternatives you've considered
I tried:

trait AsBytes { /*...*/ }
impl<T: AsRef<[u8]>> AsBytes for T { /*...*/ }
impl AsBytes for uuid::Uuid { /*...*/ }

but got:

error[E0119]: conflicting implementations of trait `AsBytes` for type `uuid::Uuid`: ...
note: upstream crates may add a new impl of trait `std::convert::AsRef<[u8]>` for type `uuid::Uuid` in future versions

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=a5cd3e47bec30ab4c71ad4270dfb3f04

Additional context
It feels this should be implemented for to cover the Interoperability princible. #191

**Is your feature request related to a problem? Please describe.** No **Describe the solution you'd like** Add some along this lines: ``` impl AsRef<[u8]> for Uuid { #[inline] fn as_ref(&self) -> &[u8] { &self.as_bytes()[..] } } ``` **Is it blocking?** No **Describe alternatives you've considered** I tried: ``` trait AsBytes { /*...*/ } impl<T: AsRef<[u8]>> AsBytes for T { /*...*/ } impl AsBytes for uuid::Uuid { /*...*/ } ``` but got: ``` error[E0119]: conflicting implementations of trait `AsBytes` for type `uuid::Uuid`: ... note: upstream crates may add a new impl of trait `std::convert::AsRef<[u8]>` for type `uuid::Uuid` in future versions ``` https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=a5cd3e47bec30ab4c71ad4270dfb3f04 **Additional context** It feels this should be implemented for to cover the Interoperability princible. #191
Waridley commented 2020-09-16 10:42:27 -05:00 (Migrated from github.com)

This error is indicating that UUID does implement AsRef<[u8]>, thus your impl AsBytes for uuid::Uuid { /*...*/ } is redundant because it's already covered by the impl for T: AsRef<[u8]>.
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=41676e06eebccf5753fb17190310db5f

This error is indicating that UUID **does** implement `AsRef<[u8]>`, thus your `impl AsBytes for uuid::Uuid { /*...*/ }` is redundant because it's already covered by the impl for `T: AsRef<[u8]>`. https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=41676e06eebccf5753fb17190310db5f
lassade commented 2020-09-17 11:45:06 -05:00 (Migrated from github.com)

You are mistaken the line println!("{:?}", uuid::Uuid::new_v4().as_bytes()); doesn't call the as_bytes from the AsBytes trait, instead it refers to this method as_bytes defined by some Uuid impl;

Changing it's name to as_bytes1 show it
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=db29daa3d9903148511de34966430627

You are mistaken the line `println!("{:?}", uuid::Uuid::new_v4().as_bytes());` doesn't call the `as_bytes` from the `AsBytes` trait, instead it refers to this method [as_bytes](https://docs.rs/uuid/0.8.1/uuid/struct.Uuid.html#method.as_bytes) defined by some Uuid impl; Changing it's name to `as_bytes1` show it https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=db29daa3d9903148511de34966430627
Waridley commented 2020-09-17 12:19:55 -05:00 (Migrated from github.com)

Ah, whoops.

Ah, whoops.
KodrAus commented 2020-12-21 02:01:18 -06:00 (Migrated from github.com)

Adding an AsRef<[u8]> for Uuid seems reasonable to me. Would somebody like to PR it?

Adding an `AsRef<[u8]> for Uuid` seems reasonable to me. Would somebody like to PR it?
Sign in to join this conversation.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
gh-uuid-rs/uuid#472
No description provided.