]> git.lizzy.rs Git - rust.git/blob - tests/ui/unsized/issue-40231-2.rs
Rollup merge of #96763 - Abdur-rahmaanJ:patch-1, r=Mark-Simulacrum
[rust.git] / tests / ui / unsized / issue-40231-2.rs
1 // check-pass
2
3 #![allow(dead_code)]
4
5 trait Structure<E>: Sized where E: Encoding {
6     type RefTarget: ?Sized;
7     type FfiPtr;
8     unsafe fn borrow_from_ffi_ptr<'a>(ptr: Self::FfiPtr) -> Option<&'a Self::RefTarget>;
9 }
10
11 enum Slice {}
12
13 impl<E> Structure<E> for Slice where E: Encoding {
14     type RefTarget = [E::Unit];
15     type FfiPtr = (*const E::FfiUnit, usize);
16     unsafe fn borrow_from_ffi_ptr<'a>(_ptr: Self::FfiPtr) -> Option<&'a Self::RefTarget> {
17         panic!()
18     }
19 }
20
21 trait Encoding {
22     type Unit: Unit;
23     type FfiUnit;
24 }
25
26 trait Unit {}
27
28 enum Utf16 {}
29
30 impl Encoding for Utf16 {
31     type Unit = Utf16Unit;
32     type FfiUnit = u16;
33 }
34
35 struct Utf16Unit(pub u16);
36
37 impl Unit for Utf16Unit {}
38
39 struct SUtf16Str {
40     _data: <Slice as Structure<Utf16>>::RefTarget,
41 }
42
43 impl SUtf16Str {
44     pub unsafe fn from_ptr<'a>(ptr: <Slice as Structure<Utf16>>::FfiPtr)
45     -> Option<&'a Self> {
46         std::mem::transmute::<Option<&[<Utf16 as Encoding>::Unit]>, _>(
47             <Slice as Structure<Utf16>>::borrow_from_ffi_ptr(ptr))
48     }
49 }
50
51 fn main() {
52     const TEXT_U16: &'static [u16] = &[];
53     let _ = unsafe { SUtf16Str::from_ptr((TEXT_U16.as_ptr(), TEXT_U16.len())).unwrap() };
54 }