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

no-std example? #316

Open
buzmeg opened this issue Nov 15, 2021 · 4 comments
Open

no-std example? #316

buzmeg opened this issue Nov 15, 2021 · 4 comments

Comments

@buzmeg
Copy link

buzmeg commented Nov 15, 2021

Is there an example of how to use snafu with no-std? The compiler is complaining that it can't find a "fail" method and I don't really understand why.

Thanks.

#![no_std]

use snafu::{ensure, Snafu};

pub type Result<T, E = Error> = core::result::Result<T, E>;

#[derive(Debug, Snafu)]
pub enum Error {
    OOMError,
}


fn foofn() -> Result<u32, Error>
{
    ensure!(1 < 0, Error::OOMError);

    Err(Error::OOMError)
}

results in:

error[E0599]: no method named `fail` found for enum `Error` in the current scope
  --> src/lib.rs:16:5
   |
9  | pub enum Error {
   | -------------- method `fail` not found for this
...
16 |     ensure!(1 < 0, Error::OOMError);
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ method not found in `Error`
   |
   = note: this error originates in the macro `ensure` (in Nightly builds, run with -Z macro-backtrace for more info)
@Enet4
Copy link
Collaborator

Enet4 commented Nov 15, 2021

Is the std feature flag disabled?

[dependencies.snafu]
version = "0.6.0"
no-default-features = true
features = ["rust_1_46"]

@buzmeg
Copy link
Author

buzmeg commented Nov 16, 2021

Well, I certainly tried to disable it:

[dependencies]
snafu = { version = "0.7.0-beta.2", features = ["rust_1_46"], default-features = false }

@shepmaster
Copy link
Owner

    ensure!(1 < 0, Error::OOMError);

You are running into the number one problem with SNAFU — crossing the context selectors and the enum variants. This line needs to be

    ensure!(1 < 0, OOMError); // SNAFU 0.6

I'd encourage you to give version 0.7.0-beta.2 a try. In 0.7, the context selectors will have a Snafu suffix by default, making this much more obvious:

    ensure!(1 < 0, OOMSnafu); // SNAFU 0.7

@shepmaster
Copy link
Owner

Oh, you are using 0.7 — that's good! Please let me know how I can improve the documentation to help avoid this problem as this specific problem is a major focus of this release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants