Skip to content

Better traits #24

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

Merged
merged 9 commits into from
Dec 7, 2019
Merged

Better traits #24

merged 9 commits into from
Dec 7, 2019

Conversation

xd009642
Copy link
Member

Rename the Image type to ImageBase and have it take T: Data and use ArrayBase<T, Ix3> as the underlying data type instead of Array3<T>.

Type alias type Image = ImageBase<OwnedRepr<T>, _> created to replicate old behaviour. Examples and tests mostly unchanged so this shouldn't affect existing users (although they should be able to improve their code easily now)

Types types types. What type of trouble do I make for myself
Look familiar? Found it was the most ergonomic way to handle all these types
Now just to refactor the tests and the new types will be ready for review
C: ColourModel,
{
/// Create an image given an existing ndarray
pub fn from_array(data: ArrayBase<T, Ix3>) -> Self {
Copy link
Member Author

Choose a reason for hiding this comment

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

removed in another PR built on top of this since I realised I'd duplicated from_data

use std::{fmt, hash, marker::PhantomData};

pub type Image<T, C> = ImageBase<OwnedRepr<T>, C>;
pub type ImageView<'a, T, C> = ImageBase<ViewRepr<&'a T>, C>;

Choose a reason for hiding this comment

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

Shame rust-lang/rfcs#2515 hasn't landed yet - it would have been nicer to phrase these as:

pub type Image<T, C> = ImageBase<impl DataMut<T>, C>;
pub type ImageView<'a, T, C> = ImageBase<impl Data<&'a T>, C>;

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, there are also bits of the code that would be nicer with const generics as well, I've just accepted eventual refactoring for the new things I want to use

fn pad(&self, image: ArrayView3<T>, padding: (usize, usize)) -> Array3<T>;
fn pad(
&self,
image: ArrayView<T, Ix3>,

Choose a reason for hiding this comment

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

I think this is too restrictive - wouldn't it be more convenient to take &ArrayBase<U, Ix3> where U: Data<Elem = T>? Is there any specific reason you are using a view?

Copy link
Member Author

@xd009642 xd009642 Dec 7, 2019

Choose a reason for hiding this comment

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

So I tried to do it but the generics got a bit hairy especially with the convolution extension trait. Just tried to do it again and got overflow evaluating the requirement `<ndarray::ArrayBase<U, ndarray::dimension::dim::Dim<[usize; 3]>> as processing::conv::ConvolutionExt<S>>::Data. It's something I'd like to do but it's a bit of a pain (or the easy way to do it isn't yet evident to me)

where
U: Data<Elem = T> + DataMut<Elem = T>,

Choose a reason for hiding this comment

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

U: DataMut<Elem = T> is enough.

where
U: Data<Elem = T> + DataMut<Elem = T>,

Choose a reason for hiding this comment

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

U: DataMut<Elem = T> should be enough.

fn get_edge_images<T>(mat: &Array3<T>) -> Result<(Array3<T>, Array3<T>), Error>
fn get_edge_images<T, U>(
mat: &ArrayBase<U, Ix3>,
) -> Result<(ArrayBase<OwnedRepr<T>, Ix3>, ArrayBase<OwnedRepr<T>, Ix3>), Error>

Choose a reason for hiding this comment

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

Result<(Array3<T>, Array3<T>), Error> would probably be more idiomatic (using the aliases where possible).

where
U: Data<Elem = T> + DataMut<Elem = T>,

Choose a reason for hiding this comment

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

U: DataMut<Elem = T>

where
U: Data<Elem = T> + DataMut<Elem = T>,

Choose a reason for hiding this comment

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

U: DataMut<Elem = T>

where
U: Data<Elem = T> + DataMut<Elem = T>,

Choose a reason for hiding this comment

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

U: DataMut<Elem = T>

Copy link

@LukeMathWalker LukeMathWalker left a comment

Choose a reason for hiding this comment

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

Some minor comments here and there, but it looks quite good to me! 👍

Also changed some types to be more idiomatic
@xd009642
Copy link
Member Author

xd009642 commented Dec 7, 2019

I've resolved everything except that padding so I'm going to merge that and leave padding as a future challenge. Thanks for the review 👍

@xd009642 xd009642 merged commit 7e4ab57 into develop Dec 7, 2019
@xd009642 xd009642 deleted the better_traits branch December 15, 2021 13:30
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.

2 participants