Does not work with lazy_static


#1

I am working on a websocket game server, which we store a singleton room vector using lazy_static
when I try to put a CollisionWorld into the room struct, it does not compile.

How can I work around this?

<lazy_static macros>:2:30: 3:59 error: the trait bound `ncollide::broad_phase::BroadPhase<na::Point2<f32>, ncollide::bounding_volume::AABB<na::Point2<f32>>, ncollide::ncollide_utils::data::uid_remap::FastKey> + 'static: core::marker::Send` is not satisfied [E0277]
<lazy_static macros>:2 use std:: sync:: ONCE_INIT ; static mut $ NAME : $ crate:: lazy:: Lazy < $ T >
<lazy_static macros>:3 = $ crate:: lazy:: Lazy ( 0 as * const $ T , ONCE_INIT ) ; }
<lazy_static macros>:21:1: 21:40 note: in this expansion of __lazy_static_create! (defined in <lazy_static macros>)
<lazy_static macros>:4:1: 5:75 note: in this expansion of lazy_static! (defined in <lazy_static macros>)
<lazy_static macros>:22:26: 22:57 note: in this expansion of lazy_static! (defined in <lazy_static macros>)
<lazy_static macros>:4:1: 5:75 note: in this expansion of lazy_static! (defined in <lazy_static macros>)
src\main.rs:79:1: 87:2 note: in this expansion of lazy_static! (defined in <lazy_static macros>)
<lazy_static macros>:2:30: 3:59 help: run `rustc --explain E0277` to see a detailed explanation
<lazy_static macros>:2:30: 3:59 note: `ncollide::broad_phase::BroadPhase<na::Point2<f32>, ncollide::bounding_volume::AABB<na::Point2<f32>>, ncollide::ncollide_utils::data::uid_remap::FastKey> + 'static` cannot be sent between threads safely
<lazy_static macros>:2:30: 3:59 note: required because it appears within the type `Box<ncollide::broad_phase::BroadPhase<na::Point2<f32>, ncollide::bounding_volume::AABB<na::Point2<f32>>, ncollide::ncollide_utils::data::uid_remap::FastKey> + 'static>`
<lazy_static macros>:2:30: 3:59 note: required because it appears within the type `ncollide::world::CollisionWorld<na::Point2<f32>, util::point::Point, i32>`
<lazy_static macros>:2:30: 3:59 note: required because it appears within the type `game::room::Room`
<lazy_static macros>:2:30: 3:59 note: required because it appears within the type `alloc::raw_vec::RawVec<game::room::Room>`
<lazy_static macros>:2:30: 3:59 note: required because it appears within the type `std::vec::Vec<game::room::Room>`
<lazy_static macros>:2:30: 3:59 note: required because it appears within the type `game::game::Game`
<lazy_static macros>:2:30: 3:59 note: required by `lazy_static::lazy::Lazy`
error: aborting due to previous error

Nphysics+specs: CollisionWorld doesn't implement Send
#2

It seems like lazy_static requires the contained type to implement Send. The CollisionWorld does not implement Send because it would require all is fields to be send-able, and that includes user-defined callbacks. And I am not sure that it is desirable (ergonomically speaking) for user callbacks to be send-able; I’ll have to think about it in more depths.

So for now, depending on why you actually need the collision world to be on a static, there might not be any satisfactory alternative. In fact, the current design will prevent you from sharing the collision world among multiple threads anyway.