Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Where possible replace ::std with ::core in codegen #73

Merged
merged 3 commits into from
Nov 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -141,7 +150,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
Expand Down Expand Up @@ -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 },
Expand Down Expand Up @@ -383,9 +392,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)"),
};

Expand Down
27 changes: 27 additions & 0 deletions tests/no_std.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#![no_std]
#![allow(dead_code)]

use auto_impl::auto_impl;

mod core {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the idea of including these at the root here just to make sure we don't accidentally use module instead of crate paths to the standard library crates?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, that was the idea


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);
}