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!("../../zerocopy-derive/tests/include.rs");
10
11 extern crate zerocopy;
12
13 use util::NotZerocopy;
14 use zerocopy::{Immutable, IntoBytes};
15
main()16 fn main() {
17 // This is adapted from #1296, which includes the following text:
18 //
19 // The compiler errors when a type is missing Immutable are somewhat
20 // misleading, although I'm not sure there's much zerocopy can do about
21 // this. An example where the compiler recommends adding a reference
22 // rather than implementing Immutable (some were even more confusing than
23 // this):
24 //
25 // error[E0277]: the trait bound `virtio::wl::CtrlVfdNewDmabuf: zerocopy::Immutable` is not satisfied
26 // --> devices/src/virtio/wl.rs:317:20
27 // |
28 // 317 | .write_obj(ctrl_vfd_new_dmabuf)
29 // | --------- ^^^^^^^^^^^^^^^^^^^ the trait `zerocopy::Immutable` is not implemented for `virtio::wl::CtrlVfdNewDmabuf`
30 // | |
31 // | required by a bound introduced by this call
32 // |
33 // note: required by a bound in `virtio::descriptor_utils::Writer::write_obj`
34 // --> devices/src/virtio/descriptor_utils.rs:536:25
35 // |
36 // 536 | pub fn write_obj<T: Immutable + IntoBytes>(&mut self, val: T) -> io::Result<()> {
37 // | ^^^^^^^^^ required by this bound in `Writer::write_obj`
38 // help: consider borrowing here
39 // |
40 // 317 | .write_obj(&ctrl_vfd_new_dmabuf)
41 // | +
42 // 317 | .write_obj(&mut ctrl_vfd_new_dmabuf)
43 // | ++++
44 //
45 // Taking the compiler's suggestion results in a different error with a
46 // recommendation to remove the reference (back to the original code).
47 //
48 // As of this writing, the described problem is still happening thanks to
49 // https://github.com/rust-lang/rust/issues/130563. We include this test so
50 // that we can capture the current behavior, but we will update it once that
51 // Rust issue is fixed.
52 Foo.write_obj(NotZerocopy(()));
53 }
54
55 struct Foo;
56
57 impl Foo {
write_obj<T: Immutable + IntoBytes>(&mut self, _val: T)58 fn write_obj<T: Immutable + IntoBytes>(&mut self, _val: T) {}
59 }
60