Error: implementation of an `unsafe` method

I try to build a package's dependency replaced as local dependency (via path) for development:

[dependencies]
egui = { version = "0.18.0", path = "../egui", default-features = false, features = [
  "tracing",
] }
winit = { version = "0.26.1", path = "../../winit" }

During the build of egui-winit it then produces a large chunk of errors (in total 215)

   Compiling winit v0.26.1 (/home/timo/git-repos/programming/rust/winit)
error: implementation of an `unsafe` method
   --> /home/timo/git-repos/programming/rust/winit/src/event.rs:550:5
    |
550 | /     pub const unsafe fn dummy() -> Self {
551 | |         DeviceId(platform_impl::DeviceId::dummy())
552 | |     }
    | |_____^
    |
    = note: requested on the command line with `-D unsafe-code`

error: implementation of an `unsafe` method
  --> /home/timo/git-repos/programming/rust/winit/src/platform_impl/linux/wayland/mod.rs:25:5
   |
25 | /     pub const unsafe fn dummy() -> Self {
26 | |         DeviceId
27 | |     }
   | |_____^

It is either implementation of an unsafe method, usage of an unsafe block or declaration of an unsafe function.

When I remove the local dependency (remove path = ../../winit) and try to build egui-winit, it builds.
When I try to build winit directly, it also builds.

I haven't dealt with unsafe before and I don't really understand how to tackle the problem.

What command are you running to build the package? The error message suggests that unsafe code has been disabled:

note: requested on the command line with `-D unsafe-code`
1 Like

I just used cargo build. Do I have to explicitly allow it? Or is there somewhere in the codebase an option where unsafe-code is explicitly rejected?

I think I found the issue. Are you building your package inside the outer egui directory? Because the workspace has a .cargo/config.toml file with the -Dunsafe_code flag:

The solution is to build your package outside of egui.

1 Like

Thanks!

I also found a mistake (?) from my side. As I said, I want to set up a dependency for development i.e. I want to have a local version of winit which I can use in a more highlevel application.

What I did before (producing the unsafe errors):

[dependencies]
egui = { version = "0.18.0", path = "../egui", default-features = false, features = [
  "tracing",
] }
winit = { version = "0.26.1", path = "../../winit" }

what I could do as well:

[dependencies]
egui = { version = "0.18.0", path = "../egui", default-features = false, features = [
  "tracing",
] }
winit = { version = "0.26.1" }

[patches.crates-io]
winit = { path = "../../winit" }

What is the preferred solution for my setup? With the latter one cargo build just works out of the box where I don't have to care about flags. Sorry for the noob questions, I'm quite new when it comes to these setups