简体   繁体   中英

How to implement a trait in the same way for many types without repetition?

Given a trait, we may want to implement it for many types.

pub trait RTypeUnit {
    fn zero() -> Self;
    fn one() -> Self;
}

impl RTypeUnit for usize { fn zero() -> usize { 0 } fn one() -> usize { 1 } }
impl RTypeUnit for isize { fn zero() -> isize { 0 } fn one() -> isize { 1 } }
impl RTypeUnit for u64 { fn zero() -> u64 { 0 } fn one() -> u64 { 1 } }
impl RTypeUnit for i64 { fn zero() -> i64 { 0 } fn one() -> i64 { 1 } }
impl RTypeUnit for u32 { fn zero() -> u32 { 0 } fn one() -> u32 { 1 } }
impl RTypeUnit for i32 { fn zero() -> i32 { 0 } fn one() -> i32 { 1 } }
impl RTypeUnit for u16 { fn zero() -> u16 { 0 } fn one() -> u16 { 1 } }
impl RTypeUnit for i16 { fn zero() -> i16 { 0 } fn one() -> i16 { 1 } }
impl RTypeUnit for u8 { fn zero() -> u8 { 0 } fn one() -> u8 { 1 } }
impl RTypeUnit for i8 { fn zero() -> i8 { 0 } fn one() -> i8 { 1 } }

What's an idiomatic way to avoid writing out functions for each type? Should I use default implementations, or maybe macros?

I am aware of the num crate for these specific methods, but I'm interested to know how to do this in the general case.

As per Rust reference :

An implementation is an item that implements a trait for a specific type.

Take a look at the implementations of Zero and One in the docs (deprecated since Rust 1.11, I removed the deprecation attributes for brevity):

pub trait Zero: Sized {
    fn zero() -> Self;
}

pub trait One: Sized {
    fn one() -> Self;
}

macro_rules! zero_one_impl {
    ($($t:ty)*) => ($(
        impl Zero for $t {
            #[inline]
            fn zero() -> Self { 0 }
        }
        impl One for $t {
            #[inline]
            fn one() -> Self { 1 }
        }
    )*)
}
zero_one_impl! { u8 u16 u32 u64 usize i8 i16 i32 i64 isize }

If the standard docs did it with macros, I doubt a better way exists.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM