iced icon indicating copy to clipboard operation
iced copied to clipboard

Implement `Default` for `Size<T>` and `Point<T>` for any `T: Default`

Open tobimd opened this issue 4 months ago • 2 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues.

Is this issue related to iced?

  • [X] My hardware is compatible and my graphics drivers are up-to-date.

What happened?

Because the type iced_core::size::Size<T> (same with iced_core::point::Point<T>) does not implement the trait std::default::Default, it creates some extremely low-stakes' bumps that I hope would be avoided if it was implemented for all types T that already implement Default, or at least for f32 which is the default for Size and Point.

As an example, the following wouldn't work:

#[derive(Default)]
struct Foo {
    ok_rectangle: iced::Rectangle, // does implement `Default`
    ok_vector: iced::Vector, // does implement `Default`
    not_size: iced::Size,
    not_point: iced::Point,
}

Just in case, I also added a discussion but only mentioned Size<T> (forgot to add Point<T>).

What is the expected behavior?

Having implementations like the following would avoid the previous problem for any T: Default, which is what already happens for iced_core::vector::Vector<T>, for example.

impl<T: std::default::Default> std::default::Default for Size<T> {
    fn default() -> Self {
        Self { width: T::default(), height: T::default() }
    }
}

impl<T: std::default::Default> std::default::Default for Point<T> {
    fn default() -> Self {
        Self { x: T::default(), y: T::default() }
    }
}

Version

crates.io release

Operating System

Linux

Do you have any log output?

No response

tobimd avatar Feb 24 '24 02:02 tobimd

Adding #[derive(Default)] to the structs in question should be enough, no need to manually implement Default.

maxicarlos08 avatar Feb 25 '24 23:02 maxicarlos08

That's true! I didn't know that the derived implementation of Default would allow that, and I don't know how I missed it even knowing that Point derived Eq. Thanks for pointing that out, my bad.

tobimd avatar Feb 27 '24 22:02 tobimd