Open CBenoit opened 6 months ago
I just hit this myself. The problem is in the bounds on the definition:
impl<T> Img<T> where T: ToOwned {
pub fn to_owned(&self) -> Img<T::Owned> {
T
here is the container type, which we would like to be the slice reference &[P]
(where P
is our pixel type), but the relevant ToOwned
implementation to copy a slice is implemented for [P]
, not &[P]
. Calling <&[P] as ToOwned>::to_owned()
just clones the slice reference. Here is a function definition that does what was presumably wanted:
pub fn img_to_owned<T, P>(this: &imgref::Img<&T>) -> imgref::Img<T::Owned>
where
T: AsRef<[P]> + ToOwned,
T::Owned: AsRef<[P]>,
{
this.map_buf(ToOwned::to_owned)
}
Unfortunately, because it's technically possible to call Img::to_owned()
, but it doesn't return the type it's supposed to, changing it to fix this bug is technically a semver breaking change.
Applications in need of a workaround could write .map_buf(ToOwned::to_owned)
or .map_buf(<[_]>::to_vec)
.
Reproducer:
The above code will not compile:
As seen in the error,
to_owned
had no effect.