[PATCH v2 11/20] rust: pin-init: internal: pin_data: enable self-referential support
From: Gary Guo
Date: Thu Oct 08 2026 - 15:34:39 EST
Enable self-referential support, and add example and test cases.
Acked-by: Benno Lossin <lossin@xxxxxxxxxx>
Signed-off-by: Gary Guo <gary@xxxxxxxxxxx>
---
rust/pin-init/examples/selfref.rs | 35 ++++++++++++++++++++++++++++++++++
rust/pin-init/internal/src/pin_data.rs | 7 -------
2 files changed, 35 insertions(+), 7 deletions(-)
diff --git a/rust/pin-init/examples/selfref.rs b/rust/pin-init/examples/selfref.rs
new file mode 100644
index 000000000000..b5cdf96b6d1c
--- /dev/null
+++ b/rust/pin-init/examples/selfref.rs
@@ -0,0 +1,35 @@
+// SPDX-License-Identifier: Apache-2.0 OR MIT
+
+#![allow(clippy::disallowed_names)]
+
+use pin_init::*;
+
+#[pin_data]
+struct SelfRef {
+ part: &'str str,
+ str: String,
+}
+
+fn use_self_ref() {
+ stack_pin_init!(let foo = pin_init!(SelfRef {
+ str: "hello world".to_owned(),
+ part: &str[..5],
+ }));
+
+ // 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());
+}
+
+fn main() {
+ use_self_ref();
+}
diff --git a/rust/pin-init/internal/src/pin_data.rs b/rust/pin-init/internal/src/pin_data.rs
index d851f13a1099..6b2c0b1f96b5 100644
--- a/rust/pin-init/internal/src/pin_data.rs
+++ b/rust/pin-init/internal/src/pin_data.rs
@@ -422,13 +422,6 @@ fn expand(
}
}
- if info.self_referential {
- dcx.error(
- &info.struct_.ident,
- "self-referential support is not fully implemented",
- );
- }
-
let struct_def = generate_struct_def(&info);
let unpin_impl = generate_unpin_impl(&info);
let drop_impl = generate_drop_impl(&info);
--
2.54.0