Deferencing an arc vs trait

error[E0277]: the trait bound `std::option::Option<&wgpu::BindGroup>: From<std::option::Option<&Arc<wgpu::BindGroup>>>` is not satisfied
   --> rend3-routine/src/forward.rs:216:41
    |
216 |                 rpass.set_bind_group(2, Some(&*bg), &[]);
    |                       --------------    ^^^^^^^^^^ the trait `From<std::option::Option<&Arc<wgpu::BindGroup>>>` is not implemented for `std::option::Option<&wgpu::BindGroup>`, which is required by `std::option::Option<&Arc<wgpu::BindGroup>>: Into<std::option::Option<&wgpu::BindGroup>>`
    |                       |
    |                       required by a bound introduced by this call
    |
    = help: the following other types implement trait `From<T>`:
              `std::option::Option<&'a T>` implements `From<&'a std::option::Option<T>>`
              `std::option::Option<&'a mut T>` implements `From<&'a mut std::option::Option<T>>`
              `std::option::Option<T>` implements `From<T>`
    = note: required for `std::option::Option<&Arc<wgpu::BindGroup>>` to implement `Into<std::option::Option<&wgpu::BindGroup>>`
note: required by a bound in `wgpu::RenderPass::<'encoder>::set_bind_group`
   --> /home/john/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wgpu-23.0.0/src/api/render_pass.rs:80:26
    |
77  |     pub fn set_bind_group<'a>(
    |            -------------- required by a bound in this associated function
...
80  |         bind_group: impl Into<Option<&'a BindGroup>>,
    |                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `RenderPass::<'encoder>::set_bind_group`

What's going on here is that, inside wgpu, the parameter to set_bind_group changed from &BindGroup to impl Into<Option<&'a BindGroup>>. So I have to modify my call accordingly.

The call used to be simply:

rpass.set_bind_group(2, bg, &[]);

bg is an Arc. It was automatically dereferenced, and that worked.

After the change making the parameter an Option, that won't work.

rpass.set_bind_group(2, Some(bg), &[]); won't work, of course, because that wraps an Option around the Arc before the Arc has been dereferenced.

rpass.set_bind_group(2, Some(&*bg), &[]); seemingly should work. That works for a simple example on Playground.

But the interaction with the trait causes a problem. What am I missing here?

Could it be that bg is &Arc<T>, not Arc<T>? Then you'd have to dereference twice. Example.

4 Likes

Probably so; deref coercion happens underneath &[mut], so the original signature wouldn't work if bg was Arc<T>.

1 Like

Thanks. Sometimes it's an Arc, and sometimes it's an &Arc. Some(&*bg) works for the former case, and Some(&**bg) works for the latter case. Before the Option was added, the compiler could handle all the de-referencing automatically.