conjure-rust icon indicating copy to clipboard operation
conjure-rust copied to clipboard

Replace educe with a custom derive macro

Open sfackler opened this issue 1 month ago • 2 comments

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.

sfackler avatar Oct 26 '25 00:10 sfackler

Generate changelog in changelog/@unreleased

Type (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 Replaced educe with a custom derive macro.

Check the box to generate changelog(s)

  • [x] Generate changelog entry

changelog-app[bot] avatar Oct 26 '25 00:10 changelog-app[bot]

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)

changelog-app[bot] avatar Oct 26 '25 00:10 changelog-app[bot]