#impl #foreign #type #macro #utilities #impl-block

macro impl_here

A macro that helps impl method for foreign types

1 unstable release

0.1.0 Dec 27, 2024

#616 in Procedural macros

Download history 119/week @ 2024-12-27

119 downloads per month

MIT/Apache

8KB
128 lines

Description

This crate is a simple macro to help impl method for foreign type. This macro converts a impl block to a trait and impl it for the foreign type.

Example

use impl_here::impl_here;

#[impl_here(ArrayTrait)]
impl<T, const L: usize> [T; L] {
    const TOTAL_SIZE: usize = size_of::<Self>();
    fn length(&self) -> usize {
        L
    }
}

assert_eq!([0.3; 125].length(), 125);
assert_eq!(<[String; 2024]>::TOTAL_SIZE, 2024 * 24);

#[impl_here(I32Square)]
impl i32 {
    pub fn square(self) -> i32 {
        self * self
    }
}
// I32Square is public because fn square is public

assert_eq!(13.square(), 13 * 13);

Dependencies

~215–660KB
~16K SLoC