impl traits for enums
It is very common to implement traits for enums if all of its variants have implemented the traits.
#![allow(unused)] fn main() { enum Data { Bin( Vec<u8> ), Text( String ), } impl AsRef<[u8]> for Data { fn as_ref( &self ) -> &[u8] { match self { Data::Bin( v ) => v.as_ref(), Data::Text( v ) => v.as_ref(), } } } }
#![allow(unused)] fn main() { impl<T0,T1> AsRef<[u8]> for Enum2<T0,T1> where T0: AsRef<[u8]> , T1: AsRef<[u8]> { fn as_ref( &self ) -> &[u8] { match self { Enum2::_0( s ) => s.as_ref(), Enum2::_1( s ) => s.as_ref(), } } } }
The basic idea is to match the variants and delegate. This library provides macros to help to avoid writing these boilerplate code:
These macros helps not repeating the where clauses and match arms of each variant.
These macros helps to omit the methods in impl blocks.