al8n/caches-rs

no_std compatability

pantsman0 opened this issue · 6 comments

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 commented

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

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 commented

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

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.

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

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?