bitcoin_internals/
lib.rs

1// SPDX-License-Identifier: CC0-1.0
2
3//! Rust Bitcoin Internal
4//!
5//! This crate is only meant to be used internally by crates in the
6//! [rust-bitcoin](https://github.com/rust-bitcoin) 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 compact_size;
42pub mod const_tools;
43pub mod error;
44pub mod macros;
45mod parse;
46pub mod script;
47pub mod slice;
48pub mod wrap_debug;
49#[cfg(feature = "serde")]
50#[macro_use]
51pub mod serde;
52pub mod const_casts;
53
54/// A conversion trait for unsigned integer types smaller than or equal to 64-bits.
55///
56/// This trait exists because [`usize`] doesn't implement `Into<u64>`. We only support 32 and 64 bit
57/// architectures because of consensus code so we can infallibly do the conversion.
58pub trait ToU64 {
59    /// Converts unsigned integer type to a [`u64`].
60    fn to_u64(self) -> u64;
61}
62
63macro_rules! impl_to_u64 {
64    ($($ty:ident),*) => {
65        $(
66            impl ToU64 for $ty { fn to_u64(self) -> u64 { self.into() } }
67        )*
68    }
69}
70impl_to_u64!(u8, u16, u32, u64);
71
72impl ToU64 for usize {
73    fn to_u64(self) -> u64 {
74        crate::const_assert!(
75            core::mem::size_of::<usize>() <= 8;
76            "platforms that have usize larger than 64 bits are not supported"
77        );
78        self as u64
79    }
80}