Skip to main content

tidecoin_internals/
lib.rs

1// SPDX-License-Identifier: CC0-1.0
2
3//! Rust Tidecoin Internal
4//!
5//! This crate is only meant to be used internally by crates in the
6//! [rust-tidecoin](https://github.com/tidecoin/rust-tidecoin) ecosystem.
7
8#![no_std]
9// Coding conventions.
10#![warn(missing_docs)]
11#![warn(deprecated_in_future)]
12#![doc(test(attr(warn(unused))))]
13
14#[cfg(feature = "alloc")]
15extern crate alloc;
16
17#[cfg(feature = "std")]
18extern crate std;
19
20#[cfg(feature = "test-serde")]
21pub extern crate serde_json;
22
23#[cfg(feature = "test-serde")]
24pub extern crate bincode;
25
26// The pub module is a workaround for strange error:
27// "macro-expanded `macro_export` macros from the current crate cannot be referred to by absolute paths"
28#[doc(hidden)]
29pub mod rust_version {
30    include!(concat!(env!("OUT_DIR"), "/rust_version.rs"));
31}
32
33#[doc(hidden)]
34pub mod _export {
35    #[cfg(feature = "alloc")]
36    pub extern crate alloc;
37}
38
39pub mod array;
40pub mod array_vec;
41pub mod const_tools;
42pub mod error;
43#[cfg(feature = "hex")]
44pub mod hex;
45pub mod macros;
46mod parse;
47pub mod script;
48pub mod slice;
49pub mod wrap_debug;
50#[cfg(feature = "serde")]
51#[macro_use]
52pub mod serde;
53pub mod const_casts;
54
55/// A conversion trait for unsigned integer types smaller than or equal to 64-bits.
56///
57/// This trait exists because [`usize`] doesn't implement `Into<u64>`. We only support 32 and 64 bit
58/// architectures because of consensus code so we can infallibly do the conversion.
59pub trait ToU64 {
60    /// Converts unsigned integer type to a [`u64`].
61    fn to_u64(self) -> u64;
62}
63
64macro_rules! impl_to_u64 {
65    ($($ty:ident),*) => {
66        $(
67            impl ToU64 for $ty { fn to_u64(self) -> u64 { self.into() } }
68        )*
69    }
70}
71impl_to_u64!(u8, u16, u32, u64);
72
73impl ToU64 for usize {
74    fn to_u64(self) -> u64 {
75        crate::const_assert!(
76            core::mem::size_of::<usize>() <= 8;
77            "platforms that have usize larger than 64 bits are not supported"
78        );
79        self as u64
80    }
81}