#extensible #lazily #traits #interface #type #struct #evaluated

plugin

Lazily evaluated, order-independent plugins for extensible types

11 releases

Uses old Rust 2015

0.2.6 Apr 22, 2015
0.2.5 Apr 3, 2015
0.2.4 Mar 31, 2015
0.2.2 Feb 28, 2015
0.0.0 Nov 11, 2014

#2921 in Rust patterns

Download history 2239/week @ 2024-03-17 2201/week @ 2024-03-24 3075/week @ 2024-03-31 1830/week @ 2024-04-07 2127/week @ 2024-04-14 2373/week @ 2024-04-21 2573/week @ 2024-04-28 2547/week @ 2024-05-05 1954/week @ 2024-05-12 2479/week @ 2024-05-19 2456/week @ 2024-05-26 2556/week @ 2024-06-02 2078/week @ 2024-06-09 2475/week @ 2024-06-16 2476/week @ 2024-06-23 1072/week @ 2024-06-30

8,396 downloads per month
Used in 183 crates (19 directly)

MIT license

8KB
113 lines

Plugin

Type-Safe, Lazily Evaluated, Plugins for Extensible Types

Plugins provide a consistent interface for mixin methods. You can use a plugin anywhere you would use a "mixin" trait and an implementation.

Example Usage

// Define a struct.
struct IntPlugin;

// Map it onto an `i32` value.
impl Assoc<i32> for IntPlugin {}

// Define the plugin evaluation function.
// `Extended` is a type that implements `Extensible`.
impl PluginFor<Extended, i32> for IntPlugin {
    fn eval(_: &Extended, _: Phantom<IntPlugin>) -> Option<i32> {
        Some(0i32)
    }
}
assert_eq!(extended.get::<IntPlugin>().unwrap(), 0i32);

To do the same thing with a trait, one could do:

trait IntProducer {
    fn get_int_value(&self) -> Option<i32>;
}

impl IntProducer for Extended {
    fn get_int_value(&self) -> Option<i32> {
        Some(0i32)
    }
}

Although using a raw trait is less code, plugins provide the following advantages:

  • Automatic caching of values. Calling a method again is a constant time operation! This is particularly useful in pipeline structures where only the extensible object is passed around.
  • A consistent interface, which also allows for neater name clash resolution. Two modules that provide PluginX can be differentiated using a module prefix.
e.get::<mod1::PluginX>();
e.get::<mod2::PluginX>();

Dependencies

~29KB