geo icon indicating copy to clipboard operation
geo copied to clipboard

Allow an AffineTransform to be constructed from a borrowed array

Open urschrei opened this issue 2 years ago • 11 comments

  • [x] I agree to follow the project's code of conduct.
  • [x] I added an entry to CHANGES.md if knowledge of this change could be valuable to users.

"borrowed array" was formerly "slice", which was wrong!

urschrei avatar Nov 03 '23 18:11 urschrei

That's not a slice, is it?

lnicola avatar Nov 03 '23 18:11 lnicola

Well this is embarrassing

urschrei avatar Nov 03 '23 18:11 urschrei

how about now?

urschrei avatar Nov 03 '23 18:11 urschrei

Better. This could probably be A: AsRef<[T; 6]>, but whatever.

Still, as prior art, Ipv4Addr only implements From<[u8; 4]>. And [T; 6] will be Copy, so I'm not sure it's worth adding it.

lnicola avatar Nov 03 '23 18:11 lnicola

Better. This could probably be A: AsRef<[T; 6]>, but whatever.

I actually tried this but got the annoying trait objects must include the dyn keyword error, which also doesn't help since the compiler says dyn AsRef<[T; 6]> + 'static doesn't impl Sized. I gave up at that point, rightly or wrongly.

urschrei avatar Nov 03 '23 18:11 urschrei

I meant

impl<T: CoordNum, A: AsRef<[T; 6]>> From<A> for AffineTransform<T> {
    fn from(arr: A) -> Self {
        let arr = arr.as_ref();
        Self::new(arr[0], arr[1], arr[2], arr[3], arr[4], arr[5])
    }
}

This would subsume both existing implementations ([T; 6] and (T, T, T, T, T, T)), as well as the one for &[T; 6].

But as I mentioned, I don't think it's necessary. Do you have some example where it helps? Because the following already works today:

fn foo(arr: &[f64; 6]) {
    let _tr = AffineTransform::from(*arr);
}

lnicola avatar Nov 03 '23 18:11 lnicola

Do you have some example where it helps? Because the following already works today

Only in avoiding the *, which was annoying me…

urschrei avatar Nov 03 '23 18:11 urschrei

But maybe your AsRef is the way to go.

urschrei avatar Nov 03 '23 18:11 urschrei

Dunno, if From<[u8; 4]> is good enough for the standard library, maybe it's good enough for us? :slightly_smiling_face:.

lnicola avatar Nov 03 '23 18:11 lnicola

Fair.

urschrei avatar Nov 03 '23 19:11 urschrei

https://github.com/georust/geo/issues/1149

frewsxcv avatar Feb 13 '24 13:02 frewsxcv