| 1 |
// Copyright 2023 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 zerocopy::transmute_ref; |
| 12 |
|
| 13 |
use crate::util::AU16; |
| 14 |
|
| 15 |
fn main() {} |
| 16 |
|
| 17 |
fn ref_dst_mutable() { |
| 18 |
// `transmute_ref!` requires that its destination type be an immutable |
| 19 |
// reference. |
| 20 |
let _: &mut u8 = transmute_ref!(&0u8); |
| 21 |
//~[msrv, stable, nightly]^ ERROR: mismatched types |
| 22 |
//~[msrv, stable, nightly]^^ ERROR: mismatched types |
| 23 |
//~[msrv, stable, nightly]^^^ ERROR: mismatched types |
| 24 |
//~[msrv, stable, nightly]^^^^ ERROR: mismatched types |
| 25 |
} |
| 26 |
|
| 27 |
// `transmute_ref!` does not support transmuting into a non-reference |
| 28 |
// destination type. |
| 29 |
const DST_NOT_A_REFERENCE: usize = transmute_ref!(&0u8); |
| 30 |
//~[msrv, stable, nightly]^ ERROR: mismatched types |
| 31 |
//~[msrv, stable, nightly]^^ ERROR: mismatched types |
| 32 |
//~[msrv, stable, nightly]^^^ ERROR: mismatched types |
| 33 |
//~[msrv, stable, nightly]^^^^ ERROR: mismatched types |
| 34 |
|
| 35 |
#[derive(zerocopy::Immutable)] |
| 36 |
#[repr(transparent)] |
| 37 |
struct DstA(AU16); |
| 38 |
|
| 39 |
// `transmute_ref` requires that the destination type implements `FromBytes` |
| 40 |
const DST_NOT_FROM_BYTES: &DstA = transmute_ref!(&AU16(0)); |
| 41 |
//~[msrv, stable, nightly]^ ERROR: the trait bound `DstA: FromBytes` is not satisfied |
| 42 |
|
| 43 |
#[derive(zerocopy::FromBytes)] |
| 44 |
#[repr(transparent)] |
| 45 |
struct DstB(AU16); |
| 46 |
|
| 47 |
// `transmute_ref` requires that the destination type implements `Immutable` |
| 48 |
const DST_NOT_IMMUTABLE: &DstB = transmute_ref!(&AU16(0)); |
| 49 |
//~[msrv, stable, nightly]^ ERROR: the trait bound `DstB: Immutable` is not satisfied |
| 50 |
|
| 51 |
// `transmute_ref!` does not support transmuting between non-reference source |
| 52 |
// and destination types. |
| 53 |
const SRC_DST_NOT_REFERENCES: usize = transmute_ref!(0usize); |
| 54 |
//~[msrv, stable, nightly]^ ERROR: mismatched types |
| 55 |
//~[msrv, stable, nightly]^^ ERROR: mismatched types |
| 56 |
//~[msrv, stable, nightly]^^^ ERROR: mismatched types |
| 57 |
//~[msrv, stable, nightly]^^^^ ERROR: mismatched types |
| 58 |
//~[msrv, stable, nightly]^^^^^ ERROR: mismatched types |
| 59 |
|
| 60 |
// `transmute_ref!` does not support transmuting from a non-reference source |
| 61 |
// type. |
| 62 |
const SRC_NOT_A_REFERENCE: &u8 = transmute_ref!(0usize); |
| 63 |
//~[msrv, stable, nightly]^ ERROR: mismatched types |
| 64 |
|
| 65 |
#[derive(zerocopy::Immutable)] |
| 66 |
#[repr(transparent)] |
| 67 |
struct SrcA(AU16); |
| 68 |
|
| 69 |
// `transmute_ref` requires that the source type implements `IntoBytes` |
| 70 |
const SRC_NOT_AS_BYTES: &AU16 = transmute_ref!(&SrcA(AU16(0))); |
| 71 |
//~[msrv, stable, nightly]^ ERROR: the trait bound `SrcA: IntoBytes` is not satisfied |
| 72 |
//~[msrv, stable, nightly]^^ ERROR: the trait bound `SrcA: IntoBytes` is not satisfied |
| 73 |
|
| 74 |
#[derive(zerocopy::IntoBytes)] |
| 75 |
#[repr(transparent)] |
| 76 |
struct SrcB(AU16); |
| 77 |
|
| 78 |
// `transmute_ref` requires that the source type implements `Immutable` |
| 79 |
const SRC_NOT_IMMUTABLE: &AU16 = transmute_ref!(&SrcB(AU16(0))); |
| 80 |
//~[msrv, stable, nightly]^ ERROR: the trait bound `SrcB: Immutable` is not satisfied |
| 81 |
//~[msrv, stable, nightly]^^ ERROR: the trait bound `SrcB: Immutable` is not satisfied |
| 82 |
|
| 83 |
// `transmute_ref!` does not support transmuting from an unsized source type. |
| 84 |
const SRC_UNSIZED: &[u8; 1] = transmute_ref!(&[0u8][..]); |
| 85 |
//~[msrv, stable]^ ERROR: the method `transmute_ref` exists for struct `Wrap<&[u8], &[u8; 1]>`, but its trait bounds were not satisfied |
| 86 |
//~[nightly]^^ ERROR: the method `transmute_ref` exists for struct `zerocopy::util::macro_util::Wrap<&[u8], &[u8; 1]>`, but its trait bounds were not satisfied |