#traits #generic #methods #object #macro #generate #object-safe

macro erased-generic-trait

Macros for generating trait objects from traits with generic methods

4 releases

0.0.4 Nov 11, 2023
0.0.3 Nov 2, 2023
0.0.2 Nov 1, 2023
0.0.1 Oct 31, 2023

#677 in Procedural macros

23 downloads per month

Apache-2.0 OR MIT

40KB
651 lines

erased-generic-trait

A Rust crate for generating trait objects from traits with generic methods.

When to use

  • When you want to make a trait obejct from a trait having generic methods.
  • When generic methods have only one kind of bounds.
  • When generic methods require 'static lifetime.
  • When you can know all types that are passed to generic methods before converting into a trait object.

Motivation

In Rust, only object safe traits can become trait objects, generic methods make them not object safe. To overcome this limitation, we can use dyn Any as parameters to take generic arguments from non-generic methods, and call the generic methods in them. We can inspect TypeIds from the 'dyn Ayn's, but we can't know concrete types from the 'TypeId's. So macros are going to inject functions calling generic methods with the concrete types, and invoke those functions according to the TypeIds. But you should know this approach is not efficient.


Please read rust documentation of this crate for more details.

Dependencies

~1–1.6MB
~28K SLoC