Debian Codegraph @

debian/missing-sources/zerocopy/tests/ui-stable/diagnostic-not-implemented-issue-1296.rs

raw
1 // Copyright 2022 The Fuchsia Authors
2 //
3 // Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0
4 // <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT
5 // license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option.
6 // This file may not be copied, modified, or distributed except according to
7 // those terms.
8
9 include!("../include.rs");
10
11 use util::NotZerocopy;
12 use zerocopy::{Immutable, IntoBytes};
13
14 fn main() {
15 // This is adapted from #1296, which includes the following text:
16 //
17 // The compiler errors when a type is missing Immutable are somewhat
18 // misleading, although I'm not sure there's much zerocopy can do about
19 // this. An example where the compiler recommends adding a reference
20 // rather than implementing Immutable (some were even more confusing than
21 // this):
22 //
23 // error[E0277]: the trait bound `virtio::wl::CtrlVfdNewDmabuf: zerocopy::Immutable` is not satisfied
24 // --> devices/src/virtio/wl.rs:317:20
25 // |
26 // 317 | .write_obj(ctrl_vfd_new_dmabuf)
27 // | --------- ^^^^^^^^^^^^^^^^^^^ the trait `zerocopy::Immutable` is not implemented for `virtio::wl::CtrlVfdNewDmabuf`
28 // | |
29 // | required by a bound introduced by this call
30 // |
31 // note: required by a bound in `virtio::descriptor_utils::Writer::write_obj`
32 // --> devices/src/virtio/descriptor_utils.rs:536:25
33 // |
34 // 536 | pub fn write_obj<T: Immutable + IntoBytes>(&mut self, val: T) -> io::Result<()> {
35 // | ^^^^^^^^^ required by this bound in `Writer::write_obj`
36 // help: consider borrowing here
37 // |
38 // 317 | .write_obj(&ctrl_vfd_new_dmabuf)
39 // | +
40 // 317 | .write_obj(&mut ctrl_vfd_new_dmabuf)
41 // | ++++
42 //
43 // Taking the compiler's suggestion results in a different error with a
44 // recommendation to remove the reference (back to the original code).
45 //
46 // As of this writing, the described problem is still happening thanks to
47 // https://github.com/rust-lang/rust/issues/130563. We include this test so
48 // that we can capture the current behavior, but we will update it once that
49 // Rust issue is fixed.
50 Foo.write_obj(NotZerocopy(()));
51 }
52
53 struct Foo;
54
55 impl Foo {
56 fn write_obj<T: Immutable + IntoBytes>(&mut self, _val: T) {}
57 }