Skip to content

Entity ergonomics#10

Open
rsammelson wants to merge 2 commits intocoriolinus:masterfrom
rsammelson:ergonomics
Open

Entity ergonomics#10
rsammelson wants to merge 2 commits intocoriolinus:masterfrom
rsammelson:ergonomics

Conversation

@rsammelson
Copy link
Contributor

This makes it substantially easier to programmatically create entities using a builder pattern, allowing only the fields in use to be specified.

Copy link
Owner

@coriolinus coriolinus left a comment

Choose a reason for hiding this comment

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

Great idea! I'm a little surprised that you implemented the builders manually--both derive_builder and typed_builder are quite good--but this lets you build in a constant context, which could definitely be convenient.

#[serde(skip_serializing_if = "Option::is_none")]
pub absolute_snapping: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub position_relative_to_grid: Option<Position>,
Copy link
Owner

Choose a reason for hiding this comment

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

Please undo non-semantic changes here, where you moved the location of position_relative_to_grid.

Comment on lines +968 to +973
pub fn new(x: f64, y: f64) -> Self {
Self {
x: R64::new(x),
y: R64::new(y),
}
}
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
pub fn new(x: f64, y: f64) -> Self {
Self {
x: R64::new(x),
y: R64::new(y),
}
}
pub fn new(x: f64, y: f64) -> Option<Self> {
Some(Self {
x: R64::try_new(x)?,
y: R64::try_new(y)?,
})
}

Requiring explicit error handling is friendlier than a behavior which sometimes panics on invalid input, except when compiled in release mode, in which case it silently does the wrong thing.

@rsammelson
Copy link
Contributor Author

Honestly using a library never crossed my mind, I just used a regex to build the functions. Maybe don't merge this as is so I can take a look at doing it automatically instead, then I can apply it to more of the objects.

@coriolinus
Copy link
Owner

One thing to mention: if you do add a proc-macro dependency such as either of the crates mentioned above, please make it an optional dependency behind a feature gate (builders or something). There exist users for whom compile speed is more important than ergonomics.

@rsammelson
Copy link
Contributor Author

After looking over the possible libraries, it doesn't look like any of them can create const functions. I think this is useful, so I'm not sure there is a builder library that fulfills the requirements.

@coriolinus
Copy link
Owner

No problem, I did not mean to imply that deriving the builders was mandatory. I can approve once you've addressed the other comments.

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