From 9ad1dfbb533c774749efd5e3bad66522c677b1a9 Mon Sep 17 00:00:00 2001 From: Zac Harrold Date: Wed, 18 Dec 2024 13:17:50 +1100 Subject: [PATCH] Make `DowncastSync` Optional The `Arc` usage in `DowncastSync` prevents compilation on platforms like `thumbv6m-none-eabi`. By placing it behind a feature flag, `sync`, we can allow compilation of the supported subset. --- Cargo.toml | 3 ++- src/lib.rs | 15 +++++++++++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 375b885..83d7e1f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,5 +12,6 @@ keywords = ["downcast", "any", "trait", "associated", "no_std"] license = "MIT/Apache-2.0" [features] -default = ["std"] +default = ["std", "sync"] std = [] +sync = [] diff --git a/src/lib.rs b/src/lib.rs index b7e3411..274351c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -35,7 +35,8 @@ //! //! Since 1.2.0, the minimum supported Rust version is 1.36 due to needing stable access to alloc. //! -//! ``` +#![cfg_attr(feature = "sync", doc = "```")] +#![cfg_attr(not(feature = "sync"), doc = "```ignore")] //! # #[macro_use] //! # extern crate downcast_rs; //! # use downcast_rs::{Downcast, DowncastSync}; @@ -72,7 +73,8 @@ //! //! # Example without generics //! -//! ``` +#![cfg_attr(feature = "sync", doc = "```")] +#![cfg_attr(not(feature = "sync"), doc = "```ignore")] //! # use std::rc::Rc; //! # use std::sync::Arc; //! // Import macro via `macro_use` pre-1.30. @@ -168,7 +170,10 @@ pub extern crate std as __std; pub extern crate alloc as __alloc; use __std::any::Any; -use __alloc::{boxed::Box, rc::Rc, sync::Arc}; +use __alloc::{boxed::Box, rc::Rc}; + +#[cfg(feature = "sync")] +use __alloc::sync::Arc; /// Supports conversion to `Any`. Traits to be extended by `impl_downcast!` must extend `Downcast`. pub trait Downcast: Any { @@ -193,6 +198,7 @@ impl Downcast for T { fn as_any_mut(&mut self) -> &mut dyn Any { self } } +#[cfg(feature = "sync")] /// Extends `Downcast` to support `Sync` traits that thus support `Arc` downcasting as well. pub trait DowncastSync: Downcast + Send + Sync { /// Convert `Arc` (where `Trait: Downcast`) to `Arc`. `Arc` can then be @@ -200,6 +206,7 @@ pub trait DowncastSync: Downcast + Send + Sync { fn into_any_arc(self: Arc) -> Arc; } +#[cfg(feature = "sync")] impl DowncastSync for T { fn into_any_arc(self: Arc) -> Arc { self } } @@ -420,7 +427,7 @@ macro_rules! impl_downcast { } -#[cfg(test)] +#[cfg(all(test, feature = "sync"))] mod test { macro_rules! test_mod { (