Skip to content
This repository was archived by the owner on Nov 30, 2022. It is now read-only.

Commit 19b74b7

Browse files
committed
use macros to impl literacy traits
1 parent 8bf2f9e commit 19b74b7

File tree

2 files changed

+56
-79
lines changed

2 files changed

+56
-79
lines changed

examples/write.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
extern crate bitcoin_hashes;
2+
3+
use bitcoin_hashes::literacy::Write;
4+
use bitcoin_hashes::{sha256d, Hash, hash_newtype, hex_fmt_impl, index_impl, serde_impl, borrow_slice_impl};
5+
6+
hash_newtype!(Txid, sha256d::Hash, 32, doc="A bitcoin transaction hash/transaction ID.");
7+
8+
fn do_it<W: Write>(mut w: W) {
9+
let _ = w.write_all(&[0u8; 1]);
10+
}
11+
12+
fn main() {
13+
let mut enc = Txid::engine();
14+
do_it(&mut enc);
15+
}

src/impls.rs

Lines changed: 41 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -19,97 +19,59 @@
1919
use {sha1, sha256, sha512, ripemd160, siphash24};
2020
use ::{HashEngine, literacy};
2121

22-
#[cfg(any(test, feature = "std"))]
23-
impl ::std::error::Error for ::Error {
24-
fn cause(&self) -> Option<&::std::error::Error> { None }
25-
fn description(&self) -> &str { "`std::error::description` is deprecated" }
26-
}
27-
28-
#[cfg(any(test, feature = "std"))]
29-
impl ::std::error::Error for ::hex::Error {
30-
fn cause(&self) -> Option<&::std::error::Error> { None }
31-
fn description(&self) -> &str { "`std::error::description` is deprecated" }
32-
}
33-
34-
impl literacy::Write for sha1::HashEngine {
35-
type Error = ();
36-
37-
fn write(&mut self, buf: &[u8]) -> ::core::result::Result<usize, ()> {
38-
self.input(buf);
39-
Ok(buf.len())
40-
}
22+
macro_rules! write_impl {
23+
($HashEngine: ty) => {
24+
impl literacy::Write for $HashEngine {
25+
type Error = ();
26+
27+
fn write(&mut self, buf: &[u8]) -> ::core::result::Result<usize, ()> {
28+
self.input(buf);
29+
Ok(buf.len())
30+
}
4131

42-
fn write_all(&mut self, buf: &[u8]) -> ::core::result::Result<(), ()> {
43-
self.write(buf)?;
44-
Ok(())
45-
}
32+
fn write_all(&mut self, buf: &[u8]) -> ::core::result::Result<(), ()> {
33+
self.write(buf)?;
34+
Ok(())
35+
}
4636

47-
fn flush(&mut self) -> ::core::result::Result<(), ()> { Ok(()) }
48-
}
37+
fn flush(&mut self) -> ::core::result::Result<(), ()> { Ok(()) }
38+
}
4939

50-
impl literacy::Write for sha256::HashEngine {
51-
type Error = ();
40+
impl literacy::Write for &mut $HashEngine {
41+
type Error = ();
5242

53-
fn write(&mut self, buf: &[u8]) -> ::core::result::Result<usize, ()> {
54-
self.input(buf);
55-
Ok(buf.len())
56-
}
43+
fn write(&mut self, buf: &[u8]) -> ::core::result::Result<usize, ()> {
44+
self.input(buf);
45+
Ok(buf.len())
46+
}
5747

58-
fn write_all(&mut self, buf: &[u8]) -> ::core::result::Result<(), ()> {
59-
self.write(buf)?;
60-
Ok(())
61-
}
48+
fn write_all(&mut self, buf: &[u8]) -> ::core::result::Result<(), ()> {
49+
self.write(buf)?;
50+
Ok(())
51+
}
6252

63-
fn flush(&mut self) -> ::core::result::Result<(), ()> { Ok(()) }
53+
fn flush(&mut self) -> ::core::result::Result<(), ()> { Ok(()) }
54+
}
55+
};
6456
}
6557

66-
impl literacy::Write for sha512::HashEngine {
67-
type Error = ();
68-
69-
fn write(&mut self, buf: &[u8]) -> ::core::result::Result<usize, ()> {
70-
self.input(buf);
71-
Ok(buf.len())
72-
}
73-
74-
fn write_all(&mut self, buf: &[u8]) -> ::core::result::Result<(), ()> {
75-
self.write(buf)?;
76-
Ok(())
77-
}
78-
79-
fn flush(&mut self) -> ::core::result::Result<(), ()> { Ok(()) }
58+
#[cfg(any(test, feature = "std"))]
59+
impl ::std::error::Error for ::Error {
60+
fn cause(&self) -> Option<&::std::error::Error> { None }
61+
fn description(&self) -> &str { "`std::error::description` is deprecated" }
8062
}
8163

82-
impl literacy::Write for ripemd160::HashEngine {
83-
type Error = ();
84-
85-
fn write(&mut self, buf: &[u8]) -> ::core::result::Result<usize, ()> {
86-
self.input(buf);
87-
Ok(buf.len())
88-
}
89-
90-
fn write_all(&mut self, buf: &[u8]) -> ::core::result::Result<(), ()> {
91-
self.write(buf)?;
92-
Ok(())
93-
}
94-
95-
fn flush(&mut self) -> ::core::result::Result<(), ()> { Ok(()) }
64+
#[cfg(any(test, feature = "std"))]
65+
impl ::std::error::Error for ::hex::Error {
66+
fn cause(&self) -> Option<&::std::error::Error> { None }
67+
fn description(&self) -> &str { "`std::error::description` is deprecated" }
9668
}
9769

98-
impl literacy::Write for siphash24::HashEngine {
99-
type Error = ();
100-
101-
fn write(&mut self, buf: &[u8]) -> ::core::result::Result<usize, ()> {
102-
self.input(buf);
103-
Ok(buf.len())
104-
}
105-
106-
fn write_all(&mut self, buf: &[u8]) -> ::core::result::Result<(), ()> {
107-
self.write(buf)?;
108-
Ok(())
109-
}
110-
111-
fn flush(&mut self) -> ::core::result::Result<(), ()> { Ok(()) }
112-
}
70+
write_impl!(sha1::HashEngine);
71+
write_impl!(sha256::HashEngine);
72+
write_impl!(sha512::HashEngine);
73+
write_impl!(ripemd160::HashEngine);
74+
write_impl!(siphash24::HashEngine);
11375

11476
#[cfg(test)]
11577
mod tests {

0 commit comments

Comments
 (0)