Merge rust-bitcoin/rust-bitcoin#1705: Avoid usage of atomic types if not supported by the target arch

2961c0c589 Exclude usage of atomic types if not supported for the target (Salvatore Ingala)

Pull request description:

  Hi! I'm still a beginner with both Rust and cross-compilation, so please take the rest with a few grains of salt!

  I'm setting up a project targeting `riscv32i-unknown-none-elf`, which seems not to support atomic types. Even with `--no-default-features` and `no-std`, I would get this error:

  ```
  error[E0432]: unresolved import `alloc::sync`
    --> rust-bitcoin/bitcoin/src/blockdata/script/mod.rs:52:12
     |
  52 | use alloc::sync::Arc;
     |            ^^^^ could not find `sync` in `alloc`

  error[E0432]: unresolved import `alloc::sync`
     --> rust-bitcoin/bitcoin/src/lib.rs:163:114
      |
  163 |     pub use alloc::{string::{String, ToString}, vec::Vec, boxed::Box, borrow::{Borrow, Cow, ToOwned}, slice, rc, sync};
      |                                                                                                                  ^^^^ no `sync` in the root
      |
      = help: consider importing one of these items instead:
              bitcoin_hashes::_export::_core::sync
              core::sync
  ```

  This PR gates the usage of `Arc` so that it's only enabled with the `std` feature.

ACKs for top commit:
  apoelstra:
    ACK 2961c0c589
  Kixunil:
    ACK 2961c0c589

Tree-SHA512: c864fb8363fca6a020472b3bdfe488bc2f33d52df98d88f0bc90ebdaa2e9f58b91cc6578eb4fc36f434ff1c50ff9c222a752bd3a72f6b9351244ad5827061e8f
This commit is contained in:
Andrew Poelstra 2023-03-17 13:46:29 +00:00
commit e658d34934
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
4 changed files with 12 additions and 2 deletions

View File

@ -23,7 +23,7 @@ fn main() {
.parse::<u64>() .parse::<u64>()
.expect("invalid Rust minor version"); .expect("invalid Rust minor version");
for activate_version in &[46, 53] { for activate_version in &[46, 53, 60] {
if minor >= *activate_version { if minor >= *activate_version {
println!("cargo:rustc-cfg=rust_v_1_{}", activate_version); println!("cargo:rustc-cfg=rust_v_1_{}", activate_version);
} }

View File

@ -49,6 +49,7 @@
//! is minimal but we may extend it in the future if needed. //! is minimal but we may extend it in the future if needed.
use alloc::rc::Rc; use alloc::rc::Rc;
#[cfg(any(not(rust_v_1_60), target_has_atomic = "ptr"))]
use alloc::sync::Arc; use alloc::sync::Arc;
use core::cmp::Ordering; use core::cmp::Ordering;
@ -279,6 +280,9 @@ impl<'a> From<&'a Script> for Cow<'a, Script> {
} }
} }
/// Note: This will fail to compile on old Rust for targets that don't support atomics
#[cfg(any(not(rust_v_1_60), target_has_atomic = "ptr"))]
#[cfg_attr(docsrs, doc(cfg(target_has_atomic = "ptr")))]
impl<'a> From<&'a Script> for Arc<Script> { impl<'a> From<&'a Script> for Arc<Script> {
fn from(value: &'a Script) -> Self { fn from(value: &'a Script) -> Self {
let rw: *const [u8] = Arc::into_raw(Arc::from(&value.0)); let rw: *const [u8] = Arc::into_raw(Arc::from(&value.0));

View File

@ -720,6 +720,9 @@ impl<T: Encodable> Encodable for rc::Rc<T> {
} }
} }
/// Note: This will fail to compile on old Rust for targets that don't support atomics
#[cfg(any(not(rust_v_1_60), target_has_atomic = "ptr"))]
#[cfg_attr(docsrs, doc(cfg(target_has_atomic = "ptr")))]
impl<T: Encodable> Encodable for sync::Arc<T> { impl<T: Encodable> Encodable for sync::Arc<T> {
fn consensus_encode<W: io::Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> { fn consensus_encode<W: io::Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
(**self).consensus_encode(w) (**self).consensus_encode(w)

View File

@ -160,7 +160,10 @@ mod io_extras {
#[rustfmt::skip] #[rustfmt::skip]
mod prelude { mod prelude {
#[cfg(all(not(feature = "std"), not(test)))] #[cfg(all(not(feature = "std"), not(test)))]
pub use alloc::{string::{String, ToString}, vec::Vec, boxed::Box, borrow::{Borrow, Cow, ToOwned}, slice, rc, sync}; pub use alloc::{string::{String, ToString}, vec::Vec, boxed::Box, borrow::{Borrow, Cow, ToOwned}, slice, rc};
#[cfg(all(not(feature = "std"), not(test), any(not(rust_v_1_60), target_has_atomic = "ptr")))]
pub use alloc::sync;
#[cfg(any(feature = "std", test))] #[cfg(any(feature = "std", test))]
pub use std::{string::{String, ToString}, vec::Vec, boxed::Box, borrow::{Borrow, Cow, ToOwned}, slice, rc, sync}; pub use std::{string::{String, ToString}, vec::Vec, boxed::Box, borrow::{Borrow, Cow, ToOwned}, slice, rc, sync};