rustserdewindows-driverrust-no-std

Duplicate lang item panic_impl writing Rust windows driver and serde


When building my driver with serde (default-features = false as per the docs) I get:

error[E0152]: duplicate lang item in crate `std` (which `serde` depends on): `panic_impl`
  |
  = note: the lang item is first defined in crate `wdk_panic` (which `drv` depends on)
  = note: first definition in `wdk_panic` loaded from \\?\C:\Users\user\git\drv\driver\target\debug\deps\libwdk_panic-4cc9693e0f1d2bfe.rmeta
  = note: second definition in `std` loaded from \\?\C:\Users\user\.rustup\toolchains\nightly-2024-10-26-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib\libstd-60ed92c773e701f8.rlib

I am using serde in my driver project as per

[package]
name = "drv"
version = "0.0.2"
edition = "2021"
build = "build.rs"

[package.metadata.wdk.driver-model]
driver-type = "WDM"

[lib]
crate-type = ["cdylib"]
test = false

[features]
default = []
nightly = ["wdk/nightly", "wdk-sys/nightly"]

[dependencies]
wdk = "0.3.0"
wdk-alloc = "0.3.0"
wdk-sys = "0.3.0"
wdk-build = "0.3.0"
wdk-panic = "0.3.0"
shared_no_std = { path = "../shared_no_std" }
serde = { version = "1.0", default-features = false, features = ["derive", "alloc"] }
serde_json = {version = "1.0.132", default-features = false, features = ["alloc"] }

[profile.dev]
panic = "abort"
lto = true

[profile.release]
panic = "abort"
lto = true

[build-dependencies]
wdk-build = "0.3.0"

In my lib.rs:

#![no_std]
extern crate alloc;

#[cfg(not(test))]
extern crate wdk_panic;
use wdk_panic as _;

use core::core_callback_notify_ps;
use ::core::ptr::null_mut;
use wdk::{nt_success, println};
#[cfg(not(test))]
use wdk_alloc::WdkAllocator;

mod ffi;
mod utils;
mod device_comms;
mod core;

#[cfg(not(test))]
#[global_allocator]
static GLOBAL_ALLOCATOR: WdkAllocator = WdkAllocator;

I think I have everything set up to use no_std from serde - I have checked also removing the dependency of the local crate shared_no_std, and I still get the same error. It seems to be re-implementing a panic_impl from std via serde?

I have also added the below to my shared_no_std (as per one stack overflow answer, but that made no change, before that i just declared it was no_std):

#![cfg_attr(not(feature = "std"), no_std)]

Output of cargo tree -i serde -e features

PS C:\Users\User\git\drv\driver> cargo tree -i serde -e features
serde v1.0.215
└── serde_json v1.0.132
    └── serde_json feature "alloc"
        ├── drv v0.0.2 (C:\Users\User\git\drv\driver)
        │   └── drv feature "default" (command-line)
        └── shared_no_std v0.0.2 (C:\Users\User\git\drv\shared_no_std)
            └── drv v0.0.2 (C:\Users\User\git\drv\driver) (*)
├── serde feature "alloc"
│   ├── drv v0.0.2 (C:\Users\User\git\drv\driver) (*)
│   └── shared_no_std v0.0.2 (C:\Users\User\git\drv\shared_no_std) (*)
│   └── serde_json feature "alloc" (*)
├── serde feature "derive"
│   ├── drv v0.0.2 (C:\Users\User\git\drv\driver) (*)
│   └── shared_no_std v0.0.2 (C:\Users\User\git\drv\shared_no_std) (*)
└── serde feature "serde_derive"
    └── serde feature "derive" (*)

serde v1.0.215
├── semver v1.0.23
│   ├── semver feature "default"
│   │   └── cargo_metadata v0.18.1
│   │       └── cargo_metadata feature "default"
│   │           └── wdk-build v0.3.0
│   │               └── wdk-build feature "default"
│   │                   └── drv v0.0.2 (C:\Users\User\git\drv\driver) (*)
│   │                   [build-dependencies]
│   │                   ├── drv v0.0.2 (C:\Users\User\git\drv\driver) (*)
│   │                   ├── wdk v0.3.0
│   │                   │   ├── wdk feature "alloc"
│   │                   │   │   └── wdk feature "default"
│   │                   │   │       └── drv v0.0.2 (C:\Users\User\git\drv\driver) (*)
│   │                   │   └── wdk feature "default" (*)
│   │                   ├── wdk-alloc v0.3.0
│   │                   │   └── wdk-alloc feature "default"
│   │                   │       └── drv v0.0.2 (C:\Users\User\git\drv\driver) (*)
│   │                   └── wdk-sys v0.3.0
│   │                       └── wdk-sys feature "default"
│   │                           ├── drv v0.0.2 (C:\Users\User\git\drv\driver) (*)
│   │                           ├── wdk v0.3.0 (*)
│   │                           └── wdk-alloc v0.3.0 (*)
│   │           [build-dependencies]
│   │           └── wdk-sys v0.3.0 (*)
│   ├── semver feature "serde"
│   │   └── cargo_metadata v0.18.1 (*)
│   └── semver feature "std"
│       └── semver feature "default" (*)
└── serde_json v1.0.132
    ├── serde_json feature "default"
    │   ├── cargo_metadata v0.18.1 (*)
    │   └── wdk-build v0.3.0 (*)
    │   [build-dependencies]
    │   └── wdk-sys v0.3.0 (*)
    ├── serde_json feature "std"
    │   └── serde_json feature "default" (*)
    └── serde_json feature "unbounded_depth"
        └── cargo_metadata v0.18.1 (*)
├── serde feature "default"
│   ├── camino v1.1.9
│   │   ├── camino feature "default"
│   │   │   ├── cargo_metadata v0.18.1 (*)
│   │   │   └── wdk-build v0.3.0 (*)
│   │   ├── camino feature "serde"
│   │   │   └── camino feature "serde1"
│   │   │       └── cargo_metadata v0.18.1 (*)
│   │   └── camino feature "serde1" (*)
│   ├── cargo-platform v0.1.8
│   │   └── cargo-platform feature "default"
│   │       └── cargo_metadata v0.18.1 (*)
│   ├── cargo_metadata v0.18.1 (*)
│   └── wdk-build v0.3.0 (*)
├── serde feature "derive"
│   ├── camino v1.1.9 (*)
│   ├── cargo_metadata v0.18.1 (*)
│   └── wdk-build v0.3.0 (*)
├── serde feature "serde_derive"
│   └── serde feature "derive" (*)
└── serde feature "std"
    ├── serde feature "default" (*)
    └── serde_json feature "std" (*)

Solution

  • Remove wdk-build from [dependencies] (leave it only in [build-dependencies]). This crate is supposed to be used during build, not at runtime, and it makes use of non-#![no_std] crates and features.