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 compatability #20

Closed
pantsman0 opened this issue Dec 2, 2023 · 7 comments
Closed

no_std compatability #20

pantsman0 opened this issue Dec 2, 2023 · 7 comments

Comments

@pantsman0
Copy link
Contributor

How do you get the crate to compile in no_std environments?

running cargo check --no-default-features --features hashbrown give the following error importing std:

error[E0433]: failed to resolve: use of undeclared crate or module `std`
 --> src\lfu\tinylfu\bloom.rs:9:19
  |
9 | const LN_2: f64 = std::f64::consts::LN_2;
  |                   ^^^ use of undeclared crate or module `std`
  |
help: consider importing one of these items
  |
7 + use core::f32::consts;
  |
7 + use core::f64::consts;
  |
help: if you import `consts`, refer to it directly
  |
9 - const LN_2: f64 = std::f64::consts::LN_2;
9 + const LN_2: f64 = consts::LN_2;
  |

error[E0599]: no method named `clone` found for struct `CountMinSketch` in the current scope
   --> src\lfu\tinylfu.rs:156:27
    |
156 |             ctr: self.ctr.clone(),
    |                           ^^^^^ method not found in `CountMinSketch`
    |
   ::: src\lfu\tinylfu\sketch\count_min_sketch_core.rs:16:1
    |
16  | pub(crate) struct CountMinSketch {
    | -------------------------------- method `clone` not found for this struct
    |
    = help: items from traits can only be used if the trait is implemented and in scope
    = note: the following trait defines an item `clone`, perhaps you need to implement it:
            candidate #1: `Clone`

Some errors have detailed explanations: E0433, E0599.
For more information about an error, try `rustc --explain E0433`.
error: could not compile `caches` (lib) due to 2 previous errors```

Running without `--features hashbrown` gives even more errors, enough that my terminal cut of the history.
https://gist.github.com/pantsman0/4d9e0178ba6270ef1e1915c39b1e3e75
@al8n
Copy link
Owner

al8n commented Dec 2, 2023

Hi, please use version 0.2.8, which fix core compatability.

@pantsman0
Copy link
Contributor Author

How are you testing the core compatibility?

When I build in no_std, I get errors because ln(), powi(), and ceil() don't exist on core::primitive::f64, and are required by lfu::tinylfu::bloom::calc_size_by_wrong_positives.

Would you accept a patch that includes optional linking to libm when std is disabled?

@al8n
Copy link
Owner

al8n commented Dec 4, 2023

How are you testing the core compatibility?

When I build in no_std, I get errors because ln(), powi(), and ceil() don't exist on core::primitive::f64, and are required by lfu::tinylfu::bloom::calc_size_by_wrong_positives.

Would you accept a patch that includes optional linking to libm when std is disabled?

Which rust version are you using? On my machine, I use 1.74.0, and the command below works. Anyway, patches are welcome, if libm works on more rust versions.

cargo build --no-default-features --features hashbrown

@pantsman0
Copy link
Contributor Author

pantsman0 commented Dec 4, 2023

yeah, building by itself seems to work OK for some reason but you can't link it into a project.

I've been able to make it fail locally using cargo asm lfu::tinylfu::bloom::calc_size_by_wrong_positives --no-default-features --features hashbrown --lib --dev with cargo-show-asm installed.

libm is definitely less desirable as it doesn't have access to the llvm intrisics that the standard library is using, but as far as I am aware it is the best portable option if you don't have access to intrinsics.

If you're OK with making no_std available only to nightly, I can add the intrinsics back in as a feature since they're available in core behind a feature gate.

EDIT: just a note that cargo asm will still work without error, but it will include calls into the std library f64 implementations.

@pantsman0
Copy link
Contributor Author

@al8n I have implemented the PR as #21. Feel free to pick and choose features you like or dislike.

Repository owner deleted a comment from jddunn Mar 1, 2024
Repository owner deleted a comment from yexuanyang Mar 4, 2024
@tsatke
Copy link

tsatke commented Apr 17, 2024

Since this ticket is still open:

I'm on x86_64-unknown-none (no_std, alloc), and I'm using the dependency as follows.
caches = { git = "https://github.com/al8n/caches-rs", default-features = false, features = ["hashbrown"] } (bd2816c).
If I don't use the hashbrown feature, I get an error because of extern crate hashbrown.

I tried the latest commit (bd2816c) since I've noticed the new version 0.2.9 which is not published yet, but even with adding libm as dependency (and extern crate libm), I still get:

error[E0432]: unresolved import `libm`
  --> ~/.cargo/git/checkouts/caches-rs-c5090a8b4544925b/bd2816c/src/polyfill.rs:23:13
   |
23 |         use libm;
   |             ^^^^ no external crate `libm`

Is there anything I am missing?
Is there anything that can be done so I can use this library with no_std on x86_64-unknown-none?

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

7 participants
@pantsman0 @al8n @tsatke and others