conjure-rust
conjure-rust copied to clipboard
Replace educe with a custom derive macro
Before this PR
Conjure-defined types using f64 need to implement traits that they would not normally be able to using Rust's built-in derive. We previously used the educe crate to be able to customize the behavior of fields containing f64. However, educe is unmaintained and has some significant correctness issues.
After this PR
==COMMIT_MSG== Replaced educe with a custom derive macro. ==COMMIT_MSG==
To minimize the complexity of the macro, we proxy the trait impls through a delegate type. This allows us to rely on Rust's built-in derive logic for all of the heavy codegen lifting:
#[derive(DeriveWith)]
#[derive_with(PartialEq)]
struct MyType(#[derive_with(with = conjure_object::private::DoubleWrapper)] f64);
// expands to (more or less)
const _: () = {
#[derive(PartialEq)]
struct __MyTypeDelegate<'a>(DoubleWrapper<&'a f64>);
impl<'a> From<&'a MyType> for __MyTypeDelegate<'a> {
fn from(v: &'a MyType>) -> Self {
__MyTypeDelegate(DoubleWrapper(&v.0))
}
}
impl PartialEq for MyType {
fn eq(&self, other: &Self) -> bool {
__MyTypeDelegate::from(self) == __MyTypeDelegate(other)
}
}
};
Possible downsides?
The conversions through the delegate type may make these trait implementations slower.
Since the derive is only intended for use by conjure_codegen, the implementation cuts some corners. For example, it doesn't work on any generic types.
Generate changelog in changelog/@unreleased
changelog/@unreleasedType (Select exactly one)
- [ ] Feature (Adding new functionality)
- [ ] Improvement (Improving existing functionality)
- [x] Fix (Fixing an issue with existing functionality)
- [ ] Break (Creating a new major version by breaking public APIs)
- [ ] Deprecation (Removing functionality in a non-breaking way)
- [ ] Migration (Automatically moving data/functionality to a new system)
Description
Check the box to generate changelog(s)
- [x] Generate changelog entry
✅ Successfully generated changelog entry!
Need to regenerate?
Simply interact with the changelog bot comment again to regenerate these entries.
📋Changelog Preview
🐛 Fixes
- Replaced educe with a custom derive macro. (#508)