From 099f54f69deab9d70776ca35a888a55052ed9ef7 Mon Sep 17 00:00:00 2001 From: Robert Kruszewski Date: Mon, 19 May 2025 10:43:00 +0100 Subject: [PATCH] Add no_std support --- Cargo.toml | 4 ++++ src/lib.rs | 22 +++++++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 0119be0..f647ab7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,3 +9,7 @@ documentation = "https://docs.rs/arcref" description = "For when you need a pointer and don't care if it's &'static T or Arc" keywords = ["arc", "arcref", "static", "pointer"] readme = "README.md" + +[features] +default = ["std"] +std = [] diff --git a/src/lib.rs b/src/lib.rs index 0869246..b2d42eb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -47,12 +47,22 @@ //! assert_eq!(logger.log(Level::INFO, "not printed"), String::new()); //! assert_eq!(logger.log(Level::WARN, "printed"), "WARN: printed"); //! ``` +#![cfg_attr(not(feature = "std"), no_std)] + +extern crate alloc; + +use alloc::sync::Arc; + +#[cfg(feature = "std")] use std::borrow::Borrow; +#[cfg(feature = "std")] use std::cmp::Ordering; +#[cfg(feature = "std")] use std::fmt::{Debug, Display}; +#[cfg(feature = "std")] use std::hash::Hash; +#[cfg(feature = "std")] use std::ops::Deref; -use std::sync::Arc; /// Either a reference-counted `Arc` or a static reference to a value. pub struct ArcRef(Inner); @@ -102,6 +112,7 @@ impl From> for ArcRef { } } +#[cfg(feature = "std")] impl Deref for ArcRef { type Target = T; @@ -113,6 +124,7 @@ impl Deref for ArcRef { } } +#[cfg(feature = "std")] impl PartialEq> for ArcRef where S: ?Sized + 'static, @@ -123,8 +135,10 @@ where } } +#[cfg(feature = "std")] impl Eq for ArcRef where T: ?Sized + 'static + Eq {} +#[cfg(feature = "std")] impl PartialOrd> for ArcRef where S: ?Sized + 'static, @@ -135,6 +149,7 @@ where } } +#[cfg(feature = "std")] impl Ord for ArcRef where T: ?Sized + 'static + Ord, @@ -144,6 +159,7 @@ where } } +#[cfg(feature = "std")] impl Hash for ArcRef where T: ?Sized + 'static + Hash, @@ -153,6 +169,7 @@ where } } +#[cfg(feature = "std")] impl Debug for ArcRef where T: ?Sized + 'static + Debug, @@ -162,6 +179,7 @@ where } } +#[cfg(feature = "std")] impl Display for ArcRef where T: ?Sized + 'static + Display, @@ -171,12 +189,14 @@ where } } +#[cfg(feature = "std")] impl AsRef for ArcRef { fn as_ref(&self) -> &T { self } } +#[cfg(feature = "std")] impl Borrow for ArcRef { fn borrow(&self) -> &T { self