greyblake / nutype

Rust newtype with guarantees 🇺🇦 🦀
MIT License
1.4k stars 23 forks source link

Fix support for `#![no_std]` and `serde` #182

Open vic1707 opened 3 weeks ago

vic1707 commented 3 weeks ago

Unable to compile in #![no_std] when using serde feature is enable and std feature is disabled. The issue happens only with Deserialize. I think the issue is the use of std::marker in the gen_impl_trait_serde_deserialize function, I'll make some tests and a PR asap.

using

[package]
name = "ttt"
version = "0.0.0"
edition = "2021"

[dependencies]
nutype = { version = "0.5.0", default-features = false, features = ["serde"] }
serde = { version = "1.0.210", default-features = false }

and

#![no_std]
use nutype::nutype;

#[nutype(derive(Deserialize))]
struct A(f32);

I get

   Compiling ttt v0.0.0 (C:\Users\vic1707\Documents\Perso\ttt)
error[E0433]: failed to resolve: could not find `std` in the list of imported crates
 --> src\lib.rs:4:1
  |
4 | #[nutype(derive(Deserialize))]
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ could not find `std` in the list of imported crates
  |
  = help: consider importing this module:
          core::marker
  = note: this error originates in the attribute macro `nutype` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0433`.
error: could not compile `ttt` (lib) due to 1 previous error

Whereas this works fine (after enabling the derive feature ofc).

#![no_std]
use serde::Deserialize;

#[derive(Deserialize)]
struct A(f32);