ink icon indicating copy to clipboard operation
ink copied to clipboard

chore: rust 2024

Open Daanvdplas opened this issue 9 months ago • 0 comments

Updates the repository to rust 2024, partially closes https://github.com/use-ink/ink-alliance/issues/43

  • [ ] as_option macro (used for event fields marked with ink::topic attribute)
#[macro_export]
#[doc(hidden)]
macro_rules! as_option {
    ( $e:expr $(,)? ) => {{
        #[allow(unused_imports)]
        use $crate::option_info::AsOptionFallback as _;
        $crate::option_info::AsOption(&$e).value()
    }};
}

How It Worked (Rust 2021): Our original macro simply borrowed the field via &$e and then called a helper (AsOption(&$e).value()), which used specialized impls or fallback traits to return an Option<&T>. Because Rust 2021 would often extend the lifetime of these temporaries implicitly, this approach worked without lifetime issues.

Stricter Lifetime Enforcement (Rust 2024): Rust 2024 enforces lifetimes more strictly. Now, when the macro creates a temporary binding inside its block—say with let tmp = $e;—and then takes a reference (&tmp), that reference’s lifetime is limited to the block. The temporary is dropped at the end of the block, making the returned reference invalid. This “lifetime extension” no longer happens automatically, and so we see errors like “temporary value dropped while borrowed.”

Current Stable‑Rust Solution and Remaining Concern: We implement our stable‑Rust solution entirely with macro pattern matching.

Concern: Because we’re relying solely on token matching rather than Rust’s type system, users must explicitly annotate Option literals—bare None won’t be recognized correctly, which risks ambiguity.

  • [ ]

Daanvdplas avatar Apr 11 '25 06:04 Daanvdplas