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 not compiling #51

Open
claravanstaden opened this issue Apr 14, 2022 · 3 comments
Open

no-std not compiling #51

claravanstaden opened this issue Apr 14, 2022 · 3 comments

Comments

@claravanstaden
Copy link

claravanstaden commented Apr 14, 2022

I am trying to use this crate with the Substrate framework, which requires the no-std feature. It looks like this crate supports running in no-std environments, if I look at Cargo.toml. However, when I try to compile the crate I get the following compiler errors:

error[E0433]: failed to resolve: could not find `prelude` in `alloc`
  --> src/lib.rs:15:20
   |
15 |     pub use alloc::prelude::v1::*;
   |                    ^^^^^^^ could not find `prelude` in `alloc`

error[E0433]: failed to resolve: use of undeclared type `Vec`
  --> src/keys.rs:58:27
   |
58 |             let mut prk = Vec::<u8>::with_capacity(1 + ikm.len());
   |                           ^^^ not found in this scope
   |
help: consider importing this struct
   |
5  | use alloc::vec::Vec;
   |

I will try to fix these in a forked version, but just wanted to get your input in case I might be missing something.

Is the crate only supported for nightly builds?

error[E0554]: `#![feature]` may not be used on the stable release channel
 --> src/lib.rs:4:5
  |
4 |     feature(alloc),
  |     ^^^^^^^^^^^^^^

error[E0554]: `#![feature]` may not be used on the stable release channel
 --> src/lib.rs:5:5
  |
5 |     feature(alloc_prelude),
  |     ^^^^^^^^^^^^^^^^^^^^^^

error[E0554]: `#![feature]` may not be used on the stable release channel
 --> src/lib.rs:6:5
  |
6 |     feature(prelude_import)
  |     ^^^^^^^^^^^^^^^^^^^^^^^
@kirk-baird
Copy link
Member

Hey sorry for the slow reply, does PR #53 resolve you issue such that it will now compile without std?

@tbraun96
Copy link

tbraun96 commented Jan 5, 2023

No, that PR did not actually solve the issue. The command #![cfg_attr(not(std), no_std)] is not equivalent to #![no_std] (see: rust-lang/rust#42190 (comment)).

Related comments:

amcl = { path = "./incubator-milagro-crypto-rust", default-features = false, features = ["bls381"]}

There, it points to https://github.com/sigp/incubator-milagro-crypto-rust/tree/057d238936c0cbbe3a59dfae6f2405db1090f474
In that repo, we see the package amcl ’s lib.rs file: https://github.com/sigp/incubator-milagro-crypto-rust/blob/057d238936c0cbbe3a59dfae6f2405db1090f474/src/lib.rs
So, what I did to try forcing a no_std environment was clone the amcl crate above, and add #![no_std] to the lib.rs and see if it would compile. It did not compile.

@claravanstaden
Copy link
Author

@tbraun96 good point!

@kirk-baird I think supporting no-std is actually a little more involved than PR #53 accomplished. Essentially a clean way to do it is so like this:

Declare imports: https://github.com/serde-rs/serde/blob/master/serde/src/lib.rs#L151
Usage: https://github.com/serde-rs/serde/blob/master/serde/src/ser/mod.rs#L110

So you toggle between std and core/alloc dependent on if feature std is enabled or not, preferably in a central place in the crate and giving it your own mod name, like lib in serde's case, or std. Then you import it (use lib::*) where any std dependency is used (which is usually everywhere 😋 ).

I attempted this for the ssz_rs crate, PR here: ralexstokes/ssz-rs#25

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