servo / euclid

Geometry primitives (basic linear algebra) for Rust
Other
458 stars 102 forks source link

Size2D: how to access width, height as `Length<T, U>`? #388

Open wez opened 4 years ago

wez commented 4 years ago

I'm looking for (sort of) the inverse of Size2D::from_lengths, but it seems like there isn't a convenient way to access the fields in this way.

The doc comment for Size2D::to_array suggests that it exposes the Length, but it just returns elements of type T.

I'd like to avoid having to manually construct a length where the fields are accessed as this is potentially error prone.

Something like this, but with much better names:

pub fn width_length(&self) -> Length<T, U> {
     Length::new(self.width)
}
pub fn height_length(&self) -> Length<T, U> {
     Length::new(self.height)
}
nical commented 4 years ago

The strongly typed getters returning Length<T, U> used to exist but were removed in part because their names weren't very ergonomic (foo.width_typed()) and more importantly becaused it seemed that nobody used them at all (maybe due to the bad ergonomics).

We could re-introduce them since this issues shows that there is some interest in using them.

My preference would be fn get_x(&self) -> Length<T, U>, fn get_width(&self) -> Length<T, U>, etc. applied consistently so that all scalar member and method accessors foo/foo() yield a raw scalar while get_foo() methods return a Length.

The documentation of to_array is a mistake. We probably deleted the wrong lines when getting rid of the typed accessor and the documentation of width_typed ended up on top of the wrong function.