summaryrefslogtreecommitdiffstats
path: root/drivers/char/rust_example/src/lib.rs
blob: 56562b840d9c77ea30a32fa1f4fdc6188fc8667a (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
// SPDX-License-Identifier: GPL-2.0

#![no_std]
#![feature(global_asm)]

use kernel::prelude::*;

struct RustExample {
    message: String,
}

impl KernelModule for RustExample {
    fn init() -> KernelResult<Self> {
        println!("Rust Example (init)");
        println!("Am I built-in? {}", !cfg!(MODULE));
        Ok(RustExample {
            message: "on the heap!".to_owned(),
        })
    }
}

impl Drop for RustExample {
    fn drop(&mut self) {
        println!("My message is {}", self.message);
        println!("Rust Example (exit)");
    }
}

kernel_module!(
    RustExample,
    author: b"Rust for Linux Contributors",
    description: b"An example kernel module written in Rust",
    license: b"GPL v2"
);