about summary refs log tree commit diff
path: root/tests/ui/consts/const-ref-to-static-linux-vtable.rs
blob: 87af63d41ab6e0d2f8dfac5b36138abe35f0b482 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//@ check-pass
//! This is the reduced version of the "Linux kernel vtable" use-case.

use std::ptr::addr_of_mut;

#[repr(C)]
struct ThisModule(i32);

trait Module {
    const THIS_MODULE_PTR: *mut ThisModule;
}

struct MyModule;

// Generated by a macro.
extern "C" {
    static mut THIS_MODULE: ThisModule;
}

// Generated by a macro.
impl Module for MyModule {
    const THIS_MODULE_PTR: *mut ThisModule = unsafe { addr_of_mut!(THIS_MODULE) };
}

struct Vtable {
    module: *mut ThisModule,
    foo_fn: fn(*mut ()) -> i32,
}

trait Foo {
    type Mod: Module;

    fn foo(&mut self) -> i32;
}

fn generate_vtable<T: Foo>() -> &'static Vtable {
    &Vtable {
        module: T::Mod::THIS_MODULE_PTR,
        foo_fn: |ptr| unsafe { &mut *ptr.cast::<T>() }.foo(),
    }
}

fn main() {}