Common convention for input data in public API

Please keep in mind that the names of variables are just for illustrative purposes.

I have a struct that can be created from a vector of arbitrary w,x pairs, or y,z usize pairs. Depending on which pair of variables it recieves, it will populate the struct in different ways. I'm aware of a few ways a user could pass in data, however they seem quite ugly.

I could create from_wx and from_yz methods of my struct which both accept Vec<(usize, usize)> for the user to map their variables to. Or, rather than using my own "from" methods, i could implement From on my struct for any type that implements Into<WXPair> or Into<YZPair>. That sounded great at first, being that I'm using the From trait, but it didn't take long for me to ask myself why I'm creating arbitrary structs... Then finally, I thought the user could implement either the trait HasWX or HasYZ where they could map the variables to methods of those traits, and I could create different init methods according to which trait they had implemented. That sounded fine, except that I cannot make functionality for both the inclusion, and exclusion of a trait. So I would still end up with differently named from_wx and from_yz methods.

I know I'm thinking way too much into this, but what would be a preferable approach to this issue?

Arbitrary in what sense? If (w, x) and (y, z) represent different kinds of input data, or maybe even just differently-structured data (with the same information content), then representing each of them with their own user-defined type is the right thing to do.

3 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.