Correct trait bounds for input that needs to convert into [u8] then Cow<'a, [u8]>

I didn't mean "remove the for<'a>". I mean "remove the entire line containing for<'a>".

That entire line would then remove the below Into part as well?

Into<Cow<'a, [u8]>>,

Which is what we had wanted no? Please clarify, perhaps you could you paste the code of what you meant for thoroughness sake?

I mean this:

pub fn insert(&mut self, item: T)
where
    T: AsRef<[u8]>,
{
    let cow: Cow<[u8]> = item.as_ref().into();
    println!("I have a cow! {:?}", cow);
    // more processing ....
    self.fragment = cow.into_owned()
}

The purpose of where bounds is to have the caller promise you that a trait implementation exists. However, you don't need a promise from the caller that &'a [u8] implements Into<Cow<'a, [u8]>>. That is already guaranteed by the standard library without the caller having to do anything.

Sweet! Thank you for providing the extra specificity, it really helps

I've managed to learn a lot from our back and forth. Your offering to lend an ear and spill some knowledge is appreciated.

Going forward what are resources I can leverage to learn more about trait bounds. I don't feel this is always "adequately" covered in the mainstream books aside from NoStarch's "Rust for Rustaceans" specifically chapter 3 where Jon speaks about "Designing Interfaces"

The O'Reilly Rust book which comes across more in-depth to me than the official Rust (NoStarch) Book has a good chapter on Utility Traits but are there further resources beyond the above mentioned?

I'm not adverse to reading the stdlib either which I've already done for things like HashMap and EmptyIterator.

One final ask, lastly, how do we know that the

The for<'a> bound can then be removed because it is always true.

once we specify the final type of [u8]?

Here's how you can see that &'a [u8] implements the trait Into<Cow<'a, [u8]>> from the documentation:

The documentation for Cow contains this:

impl<'a, T> From<&'a [T]> for Cow<'a, [T]> where
    T: Clone, 

Since u8 implements Clone, this means that Cow<'a, [u8]> implements From<&'a [u8]>. Additionally, whenever the From trait is implemented, so is the Into trait. (This can be seen on the documentation for those traits.)

Makes sense, this is really helpful to see breaking it down this way where T = [u8]

Mange tak!

Det var så lidt :slight_smile:

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.