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