use batch_impl::{batch_impl, batch_impl_only, batch_trait};
use std::collections::HashMap;
use std::rc::Rc;
use std::sync::Arc;
#[batch_impl(
[u8, u16, u32, u64, usize, i8, i16, i32, i64, isize, f32, f64] {
fn describe(&self) -> String { format!("num:{self}") }
fn is_zero(&self) -> bool { *self == Self::default() }
}
)]
trait Describe {
fn describe(&self) -> String;
fn is_zero(&self) -> bool;
}
#[batch_impl_only(
<T: Describe> [&, Box, Rc, Arc]^T #delegate(describe, is_zero){**self}
)]
trait Describe {
fn describe(&self) -> String;
fn is_zero(&self) -> bool;
}
#[batch_impl(
()^1..=4 { fn describe(&self) -> &'static str { "tuple" } }
)]
trait DescribeTuple {
fn describe(&self) -> &'static str;
}
#[batch_impl(fn(i32, u32)-String)]
trait FnReturn {}
#[batch_impl(HashMap-u8-u16)]
trait KvMarker {}
#[batch_impl(
<T> IterInfo<Item=T> Vec<T> {
fn describe(&self) -> String { format!("vec:{}", self.len()) }
}
)]
trait IterInfo {
type Item;
fn describe(&self) -> String;
}
#[batch_impl(u8 #MAX{255})]
trait HasMax {
const MAX: u8;
}
#[batch_impl(u8 #fill(name, kind){"u8"})]
trait Kind {
fn name(&self) -> &'static str;
fn kind(&self) -> &'static str;
}
trait Multi {}
unsafe trait UnsafeMark {}
batch_trait!(
Multi: u8, u16;
unsafe UnsafeMark: u32
);
#[batch_impl(*const^u32, *mut^i32)]
trait PtrMarker {}
fn main() {
assert!(0u8.is_zero());
assert_eq!(3i32.describe(), "num:3");
assert!(0.0f64.is_zero());
assert!(Box::new(0u32).is_zero());
assert!(!Rc::new(5u32).is_zero());
assert!(!Arc::new(5u32).is_zero());
assert_eq!(7u64.describe(), "num:7");
assert!(!Box::new(3i32).is_zero());
assert_eq!((1u8,).describe(), "tuple");
assert_eq!((1u8, 2u16, 3u32, 4u64).describe(), "tuple");
fn _f<T: FnReturn>(_: &T) {}
fn _k<T: KvMarker>(_: &T) {}
let fr: fn(i32, u32) -> String = |_, _| String::new();
_f(&fr);
_k(&HashMap::<u8, u16>::new());
assert_eq!(vec![1u8, 2, 3].describe(), "vec:3");
assert_eq!(<u8 as HasMax>::MAX, 255);
assert_eq!(0u8.name(), "u8");
assert_eq!(0u8.kind(), "u8");
fn _m<T: Multi>(_: &T) {}
fn _u<T: UnsafeMark>(_: &T) {}
_m(&0u8);
_m(&0u16);
_u(&0u32);
fn _p<T: PtrMarker>(_: &T) {}
let c: *const u32 = &5u32;
let m: *mut i32 = &mut 5i32;
_p(&c);
_p(&m);
println!("✔ 约 15 行 DSL → 29 个 impl,全部断言通过");
}