Attribute Macro local_impl::local_impl

source ·
#[local_impl]
Expand description

Create and impl an extension trait.

Examples

#[local_impl::local_impl]
impl<T> VecExt for Vec<T> {
    fn not_empty(&self) -> bool {
        !self.is_empty()
    }
}

let mut v = Vec::new();
assert!(!v.not_empty());
v.push(1);
assert!(v.not_empty());

Imported across modules/traits and using trait bounds.

mod other_module {
    #[local_impl::local_impl]
    impl<T: Default> VecExt for Vec<T> {
        fn push_default(&mut self) {
            self.push(T::default());
        }
    }
}

use other_module::VecExt;
let mut v: Vec<()> = Vec::new();
v.push_default();
v.push_default();
assert_eq!(v, vec![(), ()]);