From b4537fd9ac17573ded0fc6f548d0ca74bb92b844 Mon Sep 17 00:00:00 2001 From: Kevin Reid Date: Fri, 17 Jun 2022 17:27:20 -0700 Subject: [PATCH] Make ouroboros `no_std` compatible. * Mark the crate `#![no_std]`. * Replace all references to `std` with `core` or `alloc`. * Don't mention `Arc` if the target doesn't support it. * Reexport `alloc` from `ouroboros::macro_help` so that the crate calling the macro doesn't need any `extern crate alloc`. --- ouroboros/src/lib.rs | 12 ++++++++---- ouroboros_macro/src/generate/derives.rs | 4 ++-- ouroboros_macro/src/info_structures.rs | 20 ++++++++++++++++---- 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/ouroboros/src/lib.rs b/ouroboros/src/lib.rs index 45c76de..87e50ac 100644 --- a/ouroboros/src/lib.rs +++ b/ouroboros/src/lib.rs @@ -3,6 +3,7 @@ //! See the documentation of [`ouroboros_examples`](https://docs.rs/ouroboros_examples) for //! sample documentation of structs which have had the macro applied to them. +#![no_std] #![allow(clippy::needless_doctest_main)] /// This macro is used to turn a regular struct into a self-referencing one. An example: @@ -348,10 +349,12 @@ pub use ouroboros_macro::self_referencing; #[doc(hidden)] pub mod macro_help { + pub extern crate alloc; + pub use aliasable::boxed::AliasableBox; use aliasable::boxed::UniqueBox; - pub struct CheckIfTypeIsStd(std::marker::PhantomData); + pub struct CheckIfTypeIsStd(core::marker::PhantomData); macro_rules! std_type_check { ($fn_name:ident $T:ident $check_for:ty) => { @@ -361,9 +364,10 @@ pub mod macro_help { }; } - std_type_check!(is_std_box_type T std::boxed::Box); - std_type_check!(is_std_arc_type T std::sync::Arc); - std_type_check!(is_std_rc_type T std::rc::Rc); + std_type_check!(is_std_box_type T alloc::boxed::Box); + #[cfg(target_has_atomic = "ptr")] // alloc::sync is missing if this is false + std_type_check!(is_std_arc_type T alloc::sync::Arc); + std_type_check!(is_std_rc_type T alloc::rc::Rc); pub fn aliasable_boxed(data: T) -> AliasableBox { AliasableBox::from_unique(UniqueBox::new(data)) diff --git a/ouroboros_macro/src/generate/derives.rs b/ouroboros_macro/src/generate/derives.rs index 5a40841..0233a3f 100644 --- a/ouroboros_macro/src/generate/derives.rs +++ b/ouroboros_macro/src/generate/derives.rs @@ -40,10 +40,10 @@ fn impl_debug(info: &StructInfo) -> Result { } }) .collect::>(); - let trait_name = syn::parse_quote! { ::std::fmt::Debug }; + let trait_name = syn::parse_quote! { ::core::fmt::Debug }; let struct_name = &info.ident; let body = quote! { - fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { self.with(|safe_self| { f.debug_struct(stringify!(#struct_name)) #(.#fields)* diff --git a/ouroboros_macro/src/info_structures.rs b/ouroboros_macro/src/info_structures.rs index a289f37..05dc734 100644 --- a/ouroboros_macro/src/info_structures.rs +++ b/ouroboros_macro/src/info_structures.rs @@ -261,10 +261,14 @@ impl StructFieldInfo { let field_type = &self.typ; let return_ty_constructor = || match builder_type { BuilderType::AsyncSend => { - quote! { ::std::pin::Pin<::std::boxed::Box + ::core::marker::Send + 'this>> } + quote! { + ::core::pin::Pin<::ouroboros::macro_help::alloc::boxed::Box< + dyn ::core::future::Future + ::core::marker::Send + 'this>> + } } BuilderType::Async => { - quote! { ::std::pin::Pin<::std::boxed::Box + 'this>> } + quote! { ::core::pin::Pin<::ouroboros::macro_help::alloc::boxed::Box< + dyn ::core::future::Future + 'this>> } } BuilderType::Sync => quote! { #field_type }, }; @@ -280,10 +284,18 @@ impl StructFieldInfo { let field_type = &self.typ; let return_ty_constructor = || match builder_type { BuilderType::AsyncSend => { - quote! { ::std::pin::Pin<::std::boxed::Box> + ::core::marker::Send + 'this>> } + quote! { + ::core::pin::Pin<::ouroboros::macro_help::alloc::boxed::Box< + dyn ::core::future::Future> + + ::core::marker::Send + 'this>> + } } BuilderType::Async => { - quote! { ::std::pin::Pin<::std::boxed::Box> + 'this>> } + quote! { + ::core::pin::Pin<::ouroboros::macro_help::alloc::boxed::Box< + dyn ::core::future::Future> + + 'this>> + } } BuilderType::Sync => quote! { ::core::result::Result<#field_type, Error_> }, };