forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 430
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
samples: rust: add in-place initilisation sample
This is a modifid version of rust_minimal that is initialised in-place. Signed-off-by: Wedson Almeida Filho <[email protected]>
- Loading branch information
Showing
3 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |