From 1b6539ee6a9ea5ecc5214892efe5463428aaba84 Mon Sep 17 00:00:00 2001 From: Dimitri Polonski Date: Mon, 26 Oct 2020 15:57:43 +0100 Subject: [PATCH 1/3] where possible replace ::std with ::core --- src/gen.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/gen.rs b/src/gen.rs index 06724ef..bfd4900 100644 --- a/src/gen.rs +++ b/src/gen.rs @@ -141,7 +141,7 @@ fn gen_header( let relaxation = if sized_required { quote! {} } else { - quote! { + ?::std::marker::Sized } + quote! { + ?::core::marker::Sized } }; // Check if there are some `Self: Foo` bounds on methods. If so, we @@ -383,9 +383,9 @@ fn gen_fn_type_for_trait(proxy_type: &ProxyType, trait_def: &ItemTrait) -> Token // The path to the Fn-trait let fn_name = match proxy_type { - ProxyType::Fn => quote! { ::std::ops::Fn }, - ProxyType::FnMut => quote! { ::std::ops::FnMut }, - ProxyType::FnOnce => quote! { ::std::ops::FnOnce }, + ProxyType::Fn => quote! { ::core::ops::Fn }, + ProxyType::FnMut => quote! { ::core::ops::FnMut }, + ProxyType::FnOnce => quote! { ::core::ops::FnOnce }, _ => panic!("internal error in auto_impl (function contract violation)"), }; From 63e023277b8588de8835a4c76c6bebe4e4c6e7c1 Mon Sep 17 00:00:00 2001 From: Dimitri Polonski Date: Fri, 8 Jan 2021 16:06:41 +0100 Subject: [PATCH 2/3] move Box, Rc and Arc impls inside an unnamed unit const so that `extern carte alloc;` can be used. --- src/gen.rs | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/gen.rs b/src/gen.rs index bfd4900..6fb2250 100644 --- a/src/gen.rs +++ b/src/gen.rs @@ -27,9 +27,18 @@ pub(crate) fn gen_impls(proxy_types: &[ProxyType], trait_def: &syn::ItemTrait) - let header = gen_header(proxy_type, trait_def, &proxy_ty_param, &proxy_lt_param); let items = gen_items(proxy_type, trait_def, &proxy_ty_param); - tokens.append_all(quote! { - #header { #( #items )* } - }); + if matches!(proxy_type, ProxyType::Box | ProxyType::Rc | ProxyType::Arc) { + tokens.append_all(quote! { + const _: () = { + extern crate alloc; + #header { #( #items )* } + }; + }); + } else { + tokens.append_all(quote! { + #header { #( #items )* } + }); + } } tokens @@ -246,9 +255,9 @@ fn gen_header( let self_ty = match *proxy_type { ProxyType::Ref => quote! { & #proxy_lt_param #proxy_ty_param }, ProxyType::RefMut => quote! { & #proxy_lt_param mut #proxy_ty_param }, - ProxyType::Arc => quote! { ::std::sync::Arc<#proxy_ty_param> }, - ProxyType::Rc => quote! { ::std::rc::Rc<#proxy_ty_param> }, - ProxyType::Box => quote! { ::std::boxed::Box<#proxy_ty_param> }, + ProxyType::Arc => quote! { alloc::sync::Arc<#proxy_ty_param> }, + ProxyType::Rc => quote! { alloc::rc::Rc<#proxy_ty_param> }, + ProxyType::Box => quote! { alloc::boxed::Box<#proxy_ty_param> }, ProxyType::Fn => quote! { #proxy_ty_param }, ProxyType::FnMut => quote! { #proxy_ty_param }, ProxyType::FnOnce => quote! { #proxy_ty_param }, From 61874eff148e8cc6a8f54d9e263987ac095d7a81 Mon Sep 17 00:00:00 2001 From: Dimitri Polonski Date: Thu, 4 Mar 2021 01:54:51 +0100 Subject: [PATCH 3/3] add no_std test --- tests/no_std.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 tests/no_std.rs diff --git a/tests/no_std.rs b/tests/no_std.rs new file mode 100644 index 0000000..67c4d46 --- /dev/null +++ b/tests/no_std.rs @@ -0,0 +1,27 @@ +#![no_std] +#![allow(dead_code)] + +use auto_impl::auto_impl; + +mod core {} + +mod alloc {} + +struct Box; +struct Rc; +struct Arc; +struct Fn; +struct FnMut; + +#[auto_impl(&, &mut, Box, Rc, Arc)] +trait Test {} + +#[auto_impl(Fn)] +trait TestFn { + fn test(&self); +} + +#[auto_impl(FnMut)] +trait TestFnMut { + fn test(&mut self); +}