Skip to content

Commit 9f2184d

Browse files
committed
samples: rust: add in-place initialisation sample
This is a modifid version of rust_minimal that is initialised in-place. Signed-off-by: Wedson Almeida Filho <walmeida@microsoft.com>
1 parent 8ac0776 commit 9f2184d

3 files changed

Lines changed: 55 additions & 0 deletions

File tree

samples/rust/Kconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@ config SAMPLE_RUST_MINIMAL
2020

2121
If unsure, say N.
2222

23+
config SAMPLE_RUST_INPLACE
24+
tristate "Minimal in-place"
25+
help
26+
This option builds the Rust minimal module with in-place
27+
initialisation.
28+
29+
To compile this as a module, choose M here:
30+
the module will be called rust_inplace.
31+
32+
If unsure, say N.
33+
2334
config SAMPLE_RUST_PRINT
2435
tristate "Printing macros"
2536
help

samples/rust/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# SPDX-License-Identifier: GPL-2.0
22

33
obj-$(CONFIG_SAMPLE_RUST_MINIMAL) += rust_minimal.o
4+
obj-$(CONFIG_SAMPLE_RUST_INPLACE) += rust_inplace.o
45
obj-$(CONFIG_SAMPLE_RUST_PRINT) += rust_print.o
56

67
subdir-$(CONFIG_SAMPLE_RUST_HOSTPROGS) += hostprogs

samples/rust/rust_inplace.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
//! Rust minimal in-place sample.
4+
5+
use kernel::prelude::*;
6+
7+
module! {
8+
type: RustInPlace,
9+
name: "rust_inplace",
10+
author: "Rust for Linux Contributors",
11+
description: "Rust minimal in-place sample",
12+
license: "GPL",
13+
}
14+
15+
#[pin_data(PinnedDrop)]
16+
struct RustInPlace {
17+
numbers: Vec<i32>,
18+
}
19+
20+
impl kernel::InPlaceModule for RustInPlace {
21+
type Init = impl PinInit<Self, Error>;
22+
fn init(_module: &'static ThisModule) -> Self::Init {
23+
pr_info!("Rust minimal sample (init)\n");
24+
pr_info!("Am I built-in? {}\n", !cfg!(MODULE));
25+
try_pin_init!(Self {
26+
numbers: {
27+
let mut numbers = Vec::new();
28+
numbers.try_push(72)?;
29+
numbers.try_push(108)?;
30+
numbers.try_push(200)?;
31+
numbers
32+
},
33+
})
34+
}
35+
}
36+
37+
#[pinned_drop]
38+
impl PinnedDrop for RustInPlace {
39+
fn drop(self: Pin<&mut Self>) {
40+
pr_info!("My numbers are {:?}\n", self.numbers);
41+
pr_info!("Rust minimal inplace sample (exit)\n");
42+
}
43+
}

0 commit comments

Comments
 (0)