Skip to content

Commit da1a2b6

Browse files
committed
samples: rust: add in-place initialisation sample
This is a modified version of rust_minimal that is initialised in-place. Signed-off-by: Wedson Almeida Filho <[email protected]>
1 parent 484ec70 commit da1a2b6

File tree

4 files changed

+55
-1
lines changed

4 files changed

+55
-1
lines changed

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: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
fn init(_module: &'static ThisModule) -> impl PinInit<Self, Error> {
22+
pr_info!("Rust minimal sample (init)\n");
23+
pr_info!("Am I built-in? {}\n", !cfg!(MODULE));
24+
try_pin_init!(Self {
25+
numbers: {
26+
let mut numbers = Vec::new();
27+
numbers.try_push(72)?;
28+
numbers.try_push(108)?;
29+
numbers.try_push(200)?;
30+
numbers
31+
},
32+
})
33+
}
34+
}
35+
36+
#[pinned_drop]
37+
impl PinnedDrop for RustInPlace {
38+
fn drop(self: Pin<&mut Self>) {
39+
pr_info!("My numbers are {:?}\n", self.numbers);
40+
pr_info!("Rust minimal inplace sample (exit)\n");
41+
}
42+
}

scripts/Makefile.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ $(obj)/%.lst: $(src)/%.c FORCE
262262
# Compile Rust sources (.rs)
263263
# ---------------------------------------------------------------------------
264264

265-
rust_allowed_features := new_uninit,impl_trait_in_assoc_type
265+
rust_allowed_features := new_uninit,return_position_impl_trait_in_trait
266266

267267
# `--out-dir` is required to avoid temporaries being created by `rustc` in the
268268
# current working directory, which may be not accessible in the out-of-tree

0 commit comments

Comments
 (0)