1 // Copyright (C) 2024 Free Software Foundation, Inc. 2 3 // This program is free software; you can redistribute it and/or modify 4 // it under the terms of the GNU General Public License as published by 5 // the Free Software Foundation; either version 3 of the License, or 6 // (at your option) any later version. 7 // 8 // This program is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU General Public License for more details. 12 // 13 // You should have received a copy of the GNU General Public License 14 // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 16 #![allow(dead_code)] 17 #![allow(unused_variables)] 18 #![allow(unused_assignments)] 19 20 fn ignore<T>(x: T) { } 21 22 // A generic struct that is unsized if T is unsized. 23 pub struct MaybeUnsizedStruct<T: ?Sized> { 24 pub regular: u32, 25 pub rest: T, 26 } 27 28 // Same but without an ordinary part. 29 pub struct MaybeUnsizedStruct2<T: ?Sized> { 30 value: T, 31 } 32 33 fn main() { 34 // This struct is still sized because T is a fixed-length array 35 let sized_struct = &MaybeUnsizedStruct { 36 regular: 23, 37 rest: [5, 6, 7], 38 }; 39 40 // This struct is still sized because T is sized 41 let nested_sized_struct = &MaybeUnsizedStruct { 42 regular: 91, 43 rest: MaybeUnsizedStruct { 44 regular: 23, 45 rest: [5, 6, 7], 46 }, 47 }; 48 49 // This will be a fat pointer, containing the length of the final 50 // field. 51 let unsized_struct: &MaybeUnsizedStruct<[u32]> = sized_struct; 52 53 // This will also be a fat pointer, containing the length of the 54 // final field. 55 let nested_unsized_struct: 56 &MaybeUnsizedStruct<MaybeUnsizedStruct<[u32]>> = nested_sized_struct; 57 58 let alpha: MaybeUnsizedStruct2<[u8; 4]> = MaybeUnsizedStruct2 { value: *b"abc\0" }; 59 let beta: &MaybeUnsizedStruct2<[u8]> = α 60 61 let reference = &unsized_struct; 62 63 ignore(sized_struct); // set breakpoint here 64 ignore(nested_sized_struct); 65 ignore(unsized_struct); 66 ignore(nested_unsized_struct); 67 } 68