bitcoin_hashes 0.2.0

Hash functions used by rust-bitcoin which support rustc 1.14.0
Documentation
// Bitcoin Hashes Library
// Written in 2018 by
//   Andrew Poelstra <apoelstra@wpsoftware.net>
//
// To the extent possible under law, the author(s) have dedicated all
// copyright and related and neighboring rights to this software to
// the public domain worldwide. This software is distributed without
// any warranty.
//
// You should have received a copy of the CC0 Public Domain Dedication
// along with this software.
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
//

/// Circular left-shift a 32-bit word
macro_rules! circular_lshift32 (
    ($shift:expr, $w:expr) => (($w << $shift) | ($w >> (32 - $shift)))
);

macro_rules! circular_lshift64 (
    ($shift:expr, $w:expr) => (($w << $shift) | ($w >> (64 - $shift)))
);

macro_rules! hex_fmt_impl(
    ($imp:ident, $ty:ident) => (
        impl ::std::fmt::$imp for $ty {
            fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
                use hex::{format_hex, format_hex_reverse};
                if $ty::display_backward() {
                    format_hex_reverse(&self.0, f)
                } else {
                    format_hex(&self.0, f)
                }
            }
        }
    )
);

macro_rules! index_impl(
    ($ty:ty) => (
        impl ::std::ops::Index<usize> for $ty {
            type Output = u8;
            fn index(&self, index: usize) -> &u8 {
                &self.0[index]
            }
        }

        impl ::std::ops::Index<::std::ops::Range<usize>> for $ty {
            type Output = [u8];
            fn index(&self, index: ::std::ops::Range<usize>) -> &[u8] {
                &self.0[index]
            }
        }

        impl ::std::ops::Index<::std::ops::RangeFrom<usize>> for $ty {
            type Output = [u8];
            fn index(&self, index: ::std::ops::RangeFrom<usize>) -> &[u8] {
                &self.0[index]
            }
        }

        impl ::std::ops::Index<::std::ops::RangeTo<usize>> for $ty {
            type Output = [u8];
            fn index(&self, index: ::std::ops::RangeTo<usize>) -> &[u8] {
                &self.0[index]
            }
        }

        impl ::std::ops::Index<::std::ops::RangeFull> for $ty {
            type Output = [u8];
            fn index(&self, index: ::std::ops::RangeFull) -> &[u8] {
                &self.0[index]
            }
        }
    )
);

macro_rules! borrow_slice_impl(
    ($ty:ty) => (
        impl ::std::borrow::Borrow<[u8]> for $ty {
            fn borrow(&self) -> &[u8] {
                &self[..]
            }
        }
    )
);

macro_rules! write_impl(
    ($ty:ty) => (
        impl ::std::io::Write for $ty {
            fn flush(&mut self) -> ::std::io::Result<()> {
                Ok(())
            }

            #[cfg(not(feature = "fuzztarget"))]
            fn write(&mut self, inp: &[u8]) -> ::std::io::Result<usize> {
                let buf_idx = self.length % <Self as ::HashEngine>::block_size();
                let rem_len = <Self as ::HashEngine>::block_size() - buf_idx;
                let write_len = ::std::cmp::min(rem_len, inp.len());

                self.buffer[buf_idx..buf_idx + write_len].copy_from_slice(&inp[..write_len]);
                self.length += write_len;
                if self.length % <Self as ::HashEngine>::block_size() == 0 {
                    self.process_block();
                }

                Ok(write_len)
            }

            #[cfg(feature = "fuzztarget")]
            fn write(&mut self, inp: &[u8]) -> ::std::io::Result<usize> {
                for c in inp {
                    self.buffer[0] ^= *c;
                }
                self.length += inp.len();
                Ok(inp.len())
            }
        }
    )
);