summaryrefslogtreecommitdiff
path: root/rust/pin-init/examples/selfref.rs
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2026-11-21 17:22:21 -0800
committerLinus Torvalds <torvalds@linux-foundation.org>2026-11-21 17:22:21 -0800
commit4843f6873f7c4835e62f2c73ba0fd538e57a2f22 (patch)
treeee620501a1ffbad9a83141e3a70154e1fe641040 /rust/pin-init/examples/selfref.rs
downloadlinux-master.tar.gz
linux-master.zip
Merge tag 'sysctl-7.04-fixes-rc4' of git://git.kernel.org/pub/scm/linux/kernel/git/sysctl/sysctlgraftedHEADmaster
Pull sysctl fix from Joel Granados: "Fix sysctl jiffy / milliseconds conversion: - Writing a previously read value "X" shall keep the same "X" value - Remove truncation that was causing incorrect saturation of valid timeouts" * tag 'sysctl-7.04-fixes-rc4' of git://git.kernel.org/pub/scm/linux/kernel/git/sysctl/sysctl: sysctl: Do jiffy / ms conversion in ulong space
Diffstat (limited to 'rust/pin-init/examples/selfref.rs')
-rw-r--r--rust/pin-init/examples/selfref.rs60
1 files changed, 60 insertions, 0 deletions
diff --git a/rust/pin-init/examples/selfref.rs b/rust/pin-init/examples/selfref.rs
new file mode 100644
index 000000000..5212b9255
--- /dev/null
+++ b/rust/pin-init/examples/selfref.rs
@@ -0,0 +1,60 @@
+// SPDX-License-Identifier: Apache-2.0 OR MIT
+
+#![allow(clippy::disallowed_names)]
+
+use pin_init::*;
+
+#[pin_data]
+struct SelfRef {
+ #[uses('_: invariant)]
+ not_cov: Box<dyn Fn(&'str str) -> bool + 'str>,
+
+ part: &'str str,
+ str: String,
+
+ mut_part: &'mut_str mut str,
+ #[borrowed(mut)]
+ mut_str: String,
+}
+
+fn use_self_ref() {
+ stack_pin_init!(let foo = pin_init!(SelfRef {
+ str: "hello world".to_owned(),
+ part: &str[..5],
+ mut_str: "hello world".to_owned(),
+ mut_part: &mut mut_str[..5],
+ not_cov: Box::new(move |s| s == str),
+ }));
+
+ // Access via projection.
+ println!("{}", foo.as_mut().project().part);
+
+ // Access via accessor.
+ println!("{}", foo.part());
+
+ // Access via `with_project`, gives mutable reference.
+ foo.as_mut().with_project(|proj| {
+ *proj.part = &proj.str[5..];
+ });
+
+ println!("{}", foo.part());
+
+ // Access fields that mutable borrow others are similar to those of shared borrow.
+ println!("{}", foo.as_mut().project().mut_part);
+ println!("{}", foo.mut_part());
+ foo.as_mut().with_project(|proj| {
+ proj.mut_part.make_ascii_uppercase();
+ });
+
+ // Access non-covariant type using `with_` accessor.
+ foo.with_not_cov(|not_cov| {
+ not_cov("");
+ });
+
+ // Access non-covariant type using `with_project`.
+ foo.as_mut().with_project(|proj| (proj.not_cov)(proj.str));
+}
+
+fn main() {
+ use_self_ref();
+}