#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

#2903 in Rust patterns

Download history 1648/week @ 2023-12-01 1819/week @ 2023-12-08 2428/week @ 2023-12-15 1599/week @ 2023-12-22 1377/week @ 2023-12-29 1801/week @ 2024-01-05 2116/week @ 2024-01-12 2090/week @ 2024-01-19 2112/week @ 2024-01-26 1620/week @ 2024-02-02 2048/week @ 2024-02-09 2568/week @ 2024-02-16 2300/week @ 2024-02-23 2409/week @ 2024-03-01 1957/week @ 2024-03-08 1669/week @ 2024-03-15

8,706 downloads per month
Used in 191 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