Skip to content

Commit

Permalink
samples: rust: add in-place initilisation sample
Browse files Browse the repository at this point in the history
This is a modifid version of rust_minimal that is initialised in-place.

Signed-off-by: Wedson Almeida Filho <[email protected]>
  • Loading branch information
wedsonaf committed Apr 10, 2023
1 parent 1fc7eaa commit 1c03ee0
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
11 changes: 11 additions & 0 deletions samples/rust/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ config SAMPLE_RUST_MINIMAL

If unsure, say N.

config SAMPLE_RUST_INPLACE
tristate "Minimal in-place"
help
This option builds the Rust minimal module with in-place
initialisation.

To compile this as a module, choose M here:
the module will be called rust_inplace.

If unsure, say N.

config SAMPLE_RUST_PRINT
tristate "Printing macros"
help
Expand Down
1 change: 1 addition & 0 deletions samples/rust/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# SPDX-License-Identifier: GPL-2.0

obj-$(CONFIG_SAMPLE_RUST_MINIMAL) += rust_minimal.o
obj-$(CONFIG_SAMPLE_RUST_INPLACE) += rust_inplace.o
obj-$(CONFIG_SAMPLE_RUST_PRINT) += rust_print.o

subdir-$(CONFIG_SAMPLE_RUST_HOSTPROGS) += hostprogs
41 changes: 41 additions & 0 deletions samples/rust/rust_inplace.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// SPDX-License-Identifier: GPL-2.0

//! Rust minimal in-place sample.

use kernel::prelude::*;

module! {
type: RustInPlace,
name: "rust_inplace",
author: "Rust for Linux Contributors",
description: "Rust minimal in-place sample",
license: "GPL",
}

#[pin_data(PinnedDrop)]
struct RustInPlace {
numbers: Vec<i32>,
}

impl kernel::InPlaceModule for RustInPlace {
type Init = impl PinInit<Self, Error>;
fn init(_module: &'static ThisModule) -> Result<Self::Init> {
pr_info!("Rust minimal sample (init)\n");
pr_info!("Am I built-in? {}\n", !cfg!(MODULE));

let mut numbers = Vec::new();
numbers.try_push(72)?;
numbers.try_push(108)?;
numbers.try_push(200)?;

Ok(try_pin_init!(Self { numbers }))
}
}

#[pinned_drop]
impl PinnedDrop for RustInPlace {
fn drop(self: Pin<&mut Self>) {
pr_info!("My numbers are {:?}\n", self.numbers);
pr_info!("Rust minimal inplace sample (exit)\n");
}
}

0 comments on commit 1c03ee0

Please sign in to comment.