-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Should Centi
abd Milli
implement Default
?
#15
Comments
Paths
implement Default
?Centi
implement Default
?
Centi
implement Default
?Centi
abd Milli
implement Default
?
Hi! I ran some short experiments and found it a bit tricky to find a solution that works without specifically selecting scaling. The reason for the scaling is that Clipper2 is implemented with integer math to not get floating point rounding errors. I then implemented a mechanic to choose the scaling precision but left the API as f64 values only. What you can do with the current released version is to manually implement default for Foo: struct Foo {
paths: Paths,
}
impl Default for Foo {
fn default() -> Self {
Self {
paths: Paths::new(vec![]),
}
}
}
let foo = Foo::default(); I experimented with adding derived or manual Default traits but I only got some variants to pass: // TODO: this test fails util a way is found to have the compiler infer the default Centi scaler
// #[test]
// fn test_default() {
// let paths = Paths::default();
// assert_eq!(paths.len(), 0);
// }
#[test]
fn test_default_deci_precision() {
let paths = Paths::<Deci>::default();
assert_eq!(paths.len(), 0);
}
#[test]
fn test_default_as_struct_field() {
#[derive(Default)]
struct Foo {
paths: Paths,
}
let paths = Foo::default();
assert_eq!(paths.paths.len(), 0);
} I'm out of time at the moment, but please feel free to suggest concrete improvements. The best would be if adding a custom point scaler was optional and using only Paths::default() and similar was enough for anyone that wants to use the default Paths scaler. It is odd to me that the compiler complains about #[derive(Default)]
struct Foo {
paths: Paths,
}
let paths = Foo::default(); |
After seeking help from the discord channel, that is the best I come up with. #[test]
fn test_default() {
// vvvvvvv adding the angle brackets
let paths = <Paths>::default();
assert_eq!(paths.len(), 0);
} Or the type needs to be specified #[test]
fn test_default() {
let paths: Paths = Paths::default();
assert_eq!(paths.len(), 0);
} |
Exactly, it seems like the inference works much better when wrapped as a struct field. I have never seen the Though maybe requiring Is suppose that we could go for just adding the Default traits then to the minimal types needed to unlock this API? It will solve your specific problem with the Foo struct field so there at least it looks good. |
As far as I know, In short, #[derive(Default)]
struct Foo {
paths: Paths,
}
let paths = Foo::default(); and let paths: Paths = Paths::default(); Are the same as the type With that said, with my limited understanding, I don't see how it will become a trouble if |
I added the defaults impls here cc77f7b and released the crate as 0.5.1. |
Given a struct Foo
The text was updated successfully, but these errors were encountered: