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// Experimental features we need.
10#![cfg_attr(docsrs, feature(doc_auto_cfg))]
11// Coding conventions.
12#![warn(missing_docs)]
13#![doc(test(attr(warn(unused))))]
14// Exclude lints we don't think are valuable.
15#![allow(clippy::needless_question_mark)] // https://github.com/rust-bitcoin/rust-bitcoin/pull/2134
16#![allow(clippy::manual_range_contains)] // More readable than clippy's format.
17#![allow(clippy::needless_borrows_for_generic_args)] // https://github.com/rust-lang/rust-clippy/issues/12454
18
19#[cfg(feature = "alloc")]
20extern crate alloc;
21
22#[cfg(feature = "std")]
23extern crate std;
24
25#[cfg(feature = "test-serde")]
26pub extern crate serde_json;
27
28#[cfg(feature = "test-serde")]
29pub extern crate bincode;
30
31// The pub module is a workaround for strange error:
32// "macro-expanded `macro_export` macros from the current crate cannot be referred to by absolute paths"
33#[doc(hidden)]
34pub mod rust_version {
35    include!(concat!(env!("OUT_DIR"), "/rust_version.rs"));
36}
37
38pub mod array_vec;
39pub mod compact_size;
40pub mod const_tools;
41pub mod error;
42pub mod macros;
43mod parse;
44pub mod script;
45#[cfg(feature = "serde")]
46#[macro_use]
47pub mod serde;
48
49/// A conversion trait for unsigned integer types smaller than or equal to 64-bits.
50///
51/// This trait exists because [`usize`] doesn't implement `Into<u64>`. We only support 32 and 64 bit
52/// architectures because of consensus code so we can infallibly do the conversion.
53pub trait ToU64 {
54    /// Converts unsigned integer type to a [`u64`].
55    fn to_u64(self) -> u64;
56}
57
58macro_rules! impl_to_u64 {
59    ($($ty:ident),*) => {
60        $(
61            impl ToU64 for $ty { fn to_u64(self) -> u64 { self.into() } }
62        )*
63    }
64}
65impl_to_u64!(u8, u16, u32, u64);
66
67impl ToU64 for usize {
68    fn to_u64(self) -> u64 {
69        crate::const_assert!(
70            core::mem::size_of::<usize>() <= 8;
71            "platforms that have usize larger than 64 bits are not supported"
72        );
73        self as u64
74    }
75}