Skip to content

Commit

Permalink
Implement Support for no_std
Browse files Browse the repository at this point in the history
This brings no_std support to snafu. It now has a `std` feature which is
activated by default. To make no_std support as frictionless as
possible, snafu now reexports the `std::error::Error` trait as
`snafu::Error` when the feature is activated and defines its own API
compatible trait instead when it's disabled.

Resolves shepmaster#85
  • Loading branch information
CryZe committed Aug 6, 2019
1 parent 0bfff3d commit ac0f519
Show file tree
Hide file tree
Showing 5 changed files with 286 additions and 60 deletions.
16 changes: 16 additions & 0 deletions .cirrus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@ stable_test_task:
- cargo fmt --all -- --check
before_cache_script: rm -rf $CARGO_HOME/registry/index

no_std_test_task:
name: "Rust Stable (no_std)"
container:
image: rust:latest
cpu: 1
memory: 2Gi
cargo_cache:
folder: $CARGO_HOME/registry
fingerprint_script: cat Cargo.toml
setup_script:
- rustup target add thumbv6m-none-eabi
primary_test_script:
- rustc --version
- cargo build --no-default-features --target thumbv6m-none-eabi
before_cache_script: rm -rf $CARGO_HOME/registry/index

nightly_test_task:
name: "Rust Nightly"
container:
Expand Down
15 changes: 9 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ repository = "https://github.com/shepmaster/snafu"

license = "MIT OR Apache-2.0"

keywords = ["error", "ergonomic", "library"]
keywords = ["error", "ergonomic", "library", "no_std"]
categories = ["rust-patterns"]

exclude = [
Expand All @@ -23,22 +23,25 @@ exclude = [
all-features = true

[features]
default = ["rust_1_30", "backtraces"]
default = ["rust_1_30", "backtraces", "std"]

# Uses the `std::error::Error` trait instead of its own
std = []

# Adds the backtrace type
backtraces = ["snafu-derive/backtraces", "backtrace"]
backtraces = ["std", "snafu-derive/backtraces", "backtrace"]

# Add conversion to `backtrace::Backtrace`
backtrace-crate = ["backtraces"]
backtrace-crate = ["std", "backtraces"]

# Add extension traits for the futures 0.1 crate
"futures-01" = ["futures01"]
"futures-01" = ["std", "futures01"]

# New methods on `Error`; re-export of proc-macro
rust_1_30 = ["snafu-derive/rust_1_30", "doc-comment"]

# The standard library's implementation of futures
"unstable-futures" = ["futures-core-preview", "pin-project"]
"unstable-futures" = ["std", "futures-core-preview", "pin-project"]

# No public user should make use of this feature
# https://github.com/rust-lang/cargo/issues/1596
Expand Down
52 changes: 26 additions & 26 deletions snafu-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1225,7 +1225,7 @@ impl<'a> quote::ToTokens for ContextSelector<'a> {
let backtrace_field = match *backtrace_field {
Some(ref field) => {
let name = &field.name;
quote! { #name: std::default::Default::default(), }
quote! { #name: core::default::Default::default(), }
}
None => quote! {},
};
Expand All @@ -1235,7 +1235,7 @@ impl<'a> quote::ToTokens for ContextSelector<'a> {
.zip(user_fields)
.map(|(gen_ty, f)| {
let Field { ref ty, .. } = *f;
quote! { #gen_ty: std::convert::Into<#ty> }
quote! { #gen_ty: core::convert::Into<#ty> }
})
.chain(self.0.provided_where_clauses())
.collect();
Expand All @@ -1246,16 +1246,16 @@ impl<'a> quote::ToTokens for ContextSelector<'a> {
quote! {
impl<#(#generic_names,)*> #selector_name
{
#visibility fn fail<#(#original_generics_without_defaults,)* __T>(self) -> std::result::Result<__T, #parameterized_enum_name>
#visibility fn fail<#(#original_generics_without_defaults,)* __T>(self) -> core::result::Result<__T, #parameterized_enum_name>
where
#(#where_clauses),*
{
let Self { #(#names),* } = self;
let error = #enum_name::#variant_name {
#backtrace_field
#( #names: std::convert::Into::into(#names2) ),*
#( #names: core::convert::Into::into(#names2) ),*
};
std::result::Result::Err(error)
core::result::Result::Err(error)
}
}
}
Expand Down Expand Up @@ -1294,7 +1294,7 @@ impl<'a> quote::ToTokens for ContextSelector<'a> {
quote! {
impl#generics_list snafu::IntoError<#parameterized_enum_name> for #selector_name
where
#parameterized_enum_name: std::error::Error + snafu::ErrorCompat,
#parameterized_enum_name: snafu::Error + snafu::ErrorCompat,
#(#where_clauses),*
{
type Source = #source_ty;
Expand Down Expand Up @@ -1381,11 +1381,11 @@ impl<'a> quote::ToTokens for DisplayImpl<'a> {

stream.extend({
quote! {
impl<#(#original_generics),*> std::fmt::Display for #parameterized_enum_name
impl<#(#original_generics),*> core::fmt::Display for #parameterized_enum_name
where
#(#where_clauses),*
{
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
#[allow(unused_variables)]
match *self {
#(#variants_to_display)*
Expand Down Expand Up @@ -1437,13 +1437,13 @@ impl<'a> ErrorImpl<'a> {
} = *source_field;
quote! {
#enum_name::#variant_name { ref #field_name, .. } => {
std::option::Option::Some(#field_name.as_error_source())
core::option::Option::Some(#field_name.as_error_source())
}
}
}
None => {
quote! {
#enum_name::#variant_name { .. } => { std::option::Option::None }
#enum_name::#variant_name { .. } => { core::option::Option::None }
}
}
}
Expand Down Expand Up @@ -1471,7 +1471,7 @@ impl<'a> quote::ToTokens for ErrorImpl<'a> {
let variants_to_source = &self.variants_to_source();

let cause_fn = quote! {
fn cause(&self) -> Option<&std::error::Error> {
fn cause(&self) -> Option<&snafu::Error> {
use snafu::AsErrorSource;
match *self {
#(#variants_to_source)*
Expand All @@ -1481,7 +1481,7 @@ impl<'a> quote::ToTokens for ErrorImpl<'a> {

let source_fn = if cfg!(feature = "rust_1_30") {
quote! {
fn source(&self) -> Option<&(std::error::Error + 'static)> {
fn source(&self) -> Option<&(snafu::Error + 'static)> {
use snafu::AsErrorSource;
match *self {
#(#variants_to_source)*
Expand All @@ -1494,9 +1494,9 @@ impl<'a> quote::ToTokens for ErrorImpl<'a> {

stream.extend({
quote! {
impl<#(#original_generics),*> std::error::Error for #parameterized_enum_name
impl<#(#original_generics),*> snafu::Error for #parameterized_enum_name
where
Self: std::fmt::Debug + std::fmt::Display,
Self: core::fmt::Debug + core::fmt::Display,
#(#where_clauses),*
{
#description_fn
Expand Down Expand Up @@ -1537,11 +1537,11 @@ impl<'a> ErrorCompatImpl<'a> {
..
} = *backtrace_field;
quote! {
#enum_name::#variant_name { ref #field_name, .. } => { std::option::Option::Some(#field_name) }
#enum_name::#variant_name { ref #field_name, .. } => { core::option::Option::Some(#field_name) }
}
} else {
quote! {
#enum_name::#variant_name { .. } => { std::option::Option::None }
#enum_name::#variant_name { .. } => { core::option::Option::None }
}
}
}).collect()
Expand Down Expand Up @@ -1602,20 +1602,20 @@ impl StructInfo {

let description_fn = quote! {
fn description(&self) -> &str {
std::error::Error::description(&self.0)
snafu::Error::description(&self.0)
}
};

let cause_fn = quote! {
fn cause(&self) -> Option<&std::error::Error> {
std::error::Error::cause(&self.0)
fn cause(&self) -> Option<&snafu::Error> {
snafu::Error::cause(&self.0)
}
};

let source_fn = if cfg!(feature = "rust_1_30") {
quote! {
fn source(&self) -> Option<&(std::error::Error + 'static)> {
std::error::Error::source(&self.0)
fn source(&self) -> Option<&(snafu::Error + 'static)> {
snafu::Error::source(&self.0)
}
}
} else {
Expand All @@ -1633,7 +1633,7 @@ impl StructInfo {
};

let error_impl = quote! {
impl#generics std::error::Error for #parameterized_struct_name
impl#generics snafu::Error for #parameterized_struct_name
where
#(#where_clauses),*
{
Expand All @@ -1653,18 +1653,18 @@ impl StructInfo {
};

let display_impl = quote! {
impl#generics std::fmt::Display for #parameterized_struct_name
impl#generics core::fmt::Display for #parameterized_struct_name
where
#(#where_clauses),*
{
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
std::fmt::Display::fmt(&self.0, f)
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
core::fmt::Display::fmt(&self.0, f)
}
}
};

let from_impl = quote! {
impl#generics std::convert::From<#inner_type> for #parameterized_struct_name
impl#generics core::convert::From<#inner_type> for #parameterized_struct_name
where
#(#where_clauses),*
{
Expand Down
Loading

0 comments on commit ac0f519

Please sign in to comment.