wz_core/lib.rs
1//! Core traits for [wz]
2//!
3//! [wz]: https://crates.io/crates/wz
4
5#![no_std]
6
7/// Abstraction for [wz]'s stateful counters
8///
9/// This trait's identity is the unit (`()`) type
10///
11/// [wz]: https://crates.io/crates/wz
12pub trait Counter<T> {
13 fn count(&mut self, input: &[u8]);
14 fn output(&self, collector: &mut T);
15}
16
17// Identity
18impl<T> Counter<T> for () {
19 fn count(&mut self, _: &[u8]) {}
20 fn output(&self, _: &mut T) {}
21}
22
23/// Generates a collector trait with a given name
24///
25/// ```
26/// // gen_collector_trait!(FooCollector);
27/// // generates the following trait
28/// pub trait FooCollector {
29/// fn collect(&mut self, count: usize);
30/// }
31/// ```
32#[macro_export]
33macro_rules! gen_collector_trait {
34 ( $($name:tt), * ) => {
35 $(
36 pub trait $name {
37 fn collect(&mut self, count: usize);
38 }
39 )*
40 };
41}
42
43/// Implements a collector trait for usize
44///
45/// ```
46/// pub trait FooCollector {
47/// fn collect(&mut self, count: usize);
48/// }
49/// // impl_collector_usize(FooCollector);
50/// // generates the following impl block
51/// impl FooCollector for usize {
52/// fn collect(&mut self, count: usize) {
53/// *self = count;
54/// }
55/// }
56/// ```
57#[macro_export]
58macro_rules! impl_collector_usize {
59 ( $($name:ty), *) => {
60 $(
61 impl $name for usize {
62 fn collect(&mut self, count: usize) {
63 *self = count;
64 }
65 }
66 )*
67 };
68}
69
70gen_collector_trait!(
71 BytesCollector,
72 CharsCollector,
73 LinesCollector,
74 WordsCollector,
75 MaxLineLengthCollector
76);
77
78impl_collector_usize!(
79 BytesCollector,
80 CharsCollector,
81 LinesCollector,
82 WordsCollector,
83 MaxLineLengthCollector
84);