1

I need a representation for 2D coordinates on a grid. I'm aware of the «newtype» pattern that helps avoid mistakes between width and height.

#[derive(Copy, Clone, Debug)]
pub struct Width(u8);
#[derive(Copy, Clone, Debug)]
pub struct Height(u8);

#[derive(Copy, Clone, Debug)]
pub struct Location {
    pub x: Width,
    pub y: Height,
}

I really like this pattern that usesthe type system to avoid mistakes such as inadvertently swapping width and height in function parameters.

However, this approach adds verbosity to my code; I end up destructuring a lot to access the inner data since I can't really work with my newtype value:

let Width(w) = width;
let Height(h) = height;

for a in 0..h {
    for b in 0..w {
        //DO SOMETHING HERE
    }
}

Or

let Width(w) = width;
let Height(h) = height;
let mut map = vec![vec![0; w as usize]; h as usize];

I found how I could easily use arithmetic operators with my newtypes using the Add and Sub traits (even though it also has a verbosity burden) and I would like to know if there is a trait I can implement on my newtype to be able to use it in a range or to be casted as usize.

Even better, is there a way to tell the compiler that I want my newtype to behave exactly as its content, without needing to manually implement every trait by myself? Something like this maybe:

#[derive(everything_from_inner)]
struct Height(u8)
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
StyMaar
  • 43
  • 4
  • Related: http://stackoverflow.com/q/28252038/155423. Also, note that you can access inner members with `x.0`, `x.1` syntax: `let mut map = vec![vec![0; width.0 as usize]; height.0 as usize];` – Shepmaster Nov 12 '16 at 22:12
  • Thanks for the link and for the reactivity. It explains the state of Rust on the «Even better» part of my question, but not really about the traits I need to implement (even if I understand I need to implement them manually) for ranges or casting to usize. – StyMaar Nov 12 '16 at 22:21
  • That's a pretty good reason [you are only supposed to ask **one** question per post](http://meta.stackexchange.com/q/39223/281829). I'd suggest reviewing [how to ask a good question](http://stackoverflow.com/help/how-to-ask). This question is **too broad** and probably **opinion based**. Prefer to ask *narrowly focused* questions, such as "{can I,how do I} implement a trait to allow {using my type as a range,casting a type to usize}". These narrow questions allow *you* and other people to find these questions *before* asking them again — you **did** search first, right? – Shepmaster Nov 12 '16 at 22:26
  • I understand. I will edit it to rephrase the title and remove the second question, thanks ! – StyMaar Nov 12 '16 at 22:30
  • And since I actually have two different questions : the one about the Trait of ranges and the one about the casting to usize, do you think I should split it in two different post, even though I need to copy-paste the context in the two posts ? Sorry if I'm bothering you, I'm pretty new to StackOverflow :/ – StyMaar Nov 12 '16 at 22:35
  • I wouldn't worry about editing this one; but you can / should ask separate new question(s). You *might* be able to combine them, however. Something like "How can I use my newtype as if it were a usize?" and then have the concrete cases of using it in a range or in the `vec!` macro. Note that *any* type can be used in a `Range`; what's important is if the thing you are calling accepts a Range with your type in it. – Shepmaster Nov 12 '16 at 23:06
  • And it's no bother; we all were new to Stack Overflow at some point. Although some rules feel arbitrary at times, generally they all serve a purpose, usually towards keeping the site useful to the vast population of programmers (and not just one). – Shepmaster Nov 12 '16 at 23:08
  • 1
    I totally understand your remarks and the «rules» don't feel arbitrary at all in this case. I realize my question is formulated in a way that might be good to start an open discussion on Reddit, but not in a useful way for other developers to look for in the future, which is what SO is made for. Thanks for showing me that. – StyMaar Nov 12 '16 at 23:28
  • [Reddit](https://www.reddit.com/r/rust/) and [the users forum](https://users.rust-lang.org/) are indeed great places for open-ended discussion and best practices that evolve as the language does. FWIW, my hope for solving this type of problem is via [delegation](https://github.com/rust-lang/rfcs/pull/1406). – Shepmaster Nov 12 '16 at 23:29

0 Answers0