惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
The Blog of Author Tim Ferriss
Microsoft Azure Blog
Microsoft Azure Blog
S
SegmentFault 最新的问题
Schneier on Security
Schneier on Security
W
WeLiveSecurity
Webroot Blog
Webroot Blog
T
Threatpost
量子位
大猫的无限游戏
大猫的无限游戏
C
Cisco Blogs
腾讯CDC
N
News | PayPal Newsroom
T
Troy Hunt's Blog
T
Tailwind CSS Blog
Latest news
Latest news
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
The Register - Security
The Register - Security
Know Your Adversary
Know Your Adversary
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Engineering at Meta
Engineering at Meta
SecWiki News
SecWiki News
MyScale Blog
MyScale Blog
GbyAI
GbyAI
Application and Cybersecurity Blog
Application and Cybersecurity Blog
A
Arctic Wolf
The GitHub Blog
The GitHub Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Help Net Security
Help Net Security
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
有赞技术团队
有赞技术团队
NISL@THU
NISL@THU
L
LINUX DO - 最新话题
雷峰网
雷峰网
P
Privacy International News Feed
Spread Privacy
Spread Privacy
Attack and Defense Labs
Attack and Defense Labs
N
News and Events Feed by Topic
月光博客
月光博客
V
V2EX
H
Help Net Security
博客园 - 三生石上(FineUI控件)
U
Unit 42
B
Blog
PCI Perspectives
PCI Perspectives
G
GRAHAM CLULEY
Stack Overflow Blog
Stack Overflow Blog
P
Proofpoint News Feed
T
Threat Research - Cisco Blogs
Last Week in AI
Last Week in AI

The Rust Programming Language Forum - Latest topics

Beginner building a Rust backend framework (AI-assisted) — feedback appreciated C++ to Rust -- Exceptions Re-exporting a trait with a custom derive_macro Gold linker is deprecated Why is using PhantomData valid in this case? Code review for Static Pool Allocator Looking for a pool based allocator Why is this legal? What does it mean for Rustc to run "out of TLS keys"? Using async/await internally with no internal runtime, but exposing a nonblocking poll API — is this a reasonable design? Testing functions that use randomness `cargo-path`: improve coding agents&#39; ability to find Rust documentation Rust task runners Lifetime weird case Windows - USB device not detected A random rustc-ice-[...].txt file appeared Develop rust where the environment is setup in a docker Undefined Behavior: in-bounds pointer arithmetic failed: attempting to offset pointer by 20 bytes, but got alloc238 which is only 1 byte from the end of the allocation Unbug 0.5 - Runtime debug assertions Is there a tiny error in section 6.2 Reference types? Lifetime woes implementing ratatui::Widget for a reference Rusqlite + Chrono: How do I simplify code to obtain chrono datetime value From OOP to Rust – struggling with code organization and data structure design Way to avoid a self-referential struct Rust RF and audio resources/communities Whyhttp - HTTP mocks that fail where the bug actually is Using tokio channel permits in a tower service Arc::increment_strong_count design question (cross-post) `&T`, `&mut T`, `Pin<&mut T>` and `&Cell<T>`: Ways of Borrowing a `T` Ratatui detect arrow key press and release Feedback about post in medium Debugger not showing/offering variables? nestrs: a NestJS-style framework for Rust built on Axum and Tower Confusion about struct variance with &mut dyn Narwhal v0.6.0 – message broker for edge apps, now with channel persistence Quiche client sent message not received at the server side "mysql" "Packets out of sync" - workaround Consistency f64 and std::Simd<f64, N> Signal semantics for Rust Snowflake-gen 1.0 - Configurable Snowflake ID generator with thread-local global API How do I do hmr in rust Diving into Rust: My Journey So Far and a Few Hiccups Celerity 0.1.1: pure Rust ZMTP 3.1 with a sans-IO core, Tokio transport, and CURVE-RS How to call struct method as function pointer in loop Loops inside loops and mutable reference on Iterator VSCode - rust-analyzer - annoying re-builds due to RUSTC_BOOTSTRAP changed Borsh v1.6.1 example What's everyone working on this week (17/2026)? Cargo test's verbose output - can I quiet it? Sem: semantic entity level version control CLI built with tree-sitter How do i get all the words in a string and convert it into a vector? Looking for feedback on my first tokenizer in Rust Frostmirror - (lightweight airgap alternative inspired from panamax and romt) - an offline cargo repository for airgap system that can be deployed through a docker image Does "ill-formed, no diagnostic required" exist in Rust? If a program is built with software floating-point emulation, is it affected by hardware floating-point environment? Rustc-link-lib static library How does crates.io and docs.rs store crates? Various input in one line error[E0080]: index out of bounds error when trying to install LuaCAD CSV to SQLite - Is it better to write records as they are being read Looking for feedback for a graph-based task scheduler Confusion: struct directly followed by impl? This is my first project. I invented a new algorithm that even AI don't know. It's about bigint. And I'm 15. Thanks for watching. By the way, my crate named `nna` Why Windows rustc uses a different color scheme? Associate a struct with every enum variant Which Windows env var is responsible for DNS resolition? Clap completion question Is RMW operation less prone to reading stale values than a pure load? Crates with excluded proprietary parts Why are builds of my crate's test not consistent (cargo build vs cargo test vs VSCode-Test-Explorer) Why there is no downcast method Type inference of generic parameters How to make Rust as vertical as possible? Cyw43 on STM32H723 How to use build-analysis feature? Why can't I upgrade a known-exclusive &T to &mut T? Issues with Boxed trait objects Rust-analyzer Language Server Loops with accumulated state confuse me LoMux: Adobe Media Encoder Alternative (First Rust Project) Cbor-core: A deterministic CBOR::Core encoder/decoder Numa - a DNS resolver from scratch How should I deal with copying and renaming in monitoring of files? Request get to api.github.com Compiler Preprocessing Optimization Lifetime trait bounds issue High-Performance API Gateway Implemented a Rust-based CLI tool that supports Ollama's local LLMs Egui_tracing 0.3 — a live tracing/log viewer widget for egui apps Elevation-kit capstone project Panic-free-friendly standard library Work around library with private trait in public trait bound My custom Tab implementation with Slint How to express this complex struct definition which uses closures and multiple layers of higher-ranked lifetimes in Rust? 30K lines of no_std Rust: a bare-metal ARM64 hypervisor that replaces Google's Hafnium SPMC Imperartive or functional? Any improvements? Loop unrolling in manually written loops vs iterators (AVX512) Announcing cachekit 0.7 — pluggable cache policies and arena-backed primitives Need some suggestions Having Multiple Mutable References via Iterators
Typing is fun! Another crate for self-referential structs
sshockwave · 2026-04-17 · via The Rust Programming Language Forum - Latest topics

April 16, 2026, 4:04pm 1

I recently came across self-referential structs and found some execellent solutions like self_cell and yoke. They should probably be the go-to choice these days, but I still gave a thought about how these patterns should really work.

My idea is to use HRTB on the function for<'a> fn derive(&'a Owner)->View<'a> to require this function prove that View<'a> is valid for all lifetimes of &'a owner, which happens to include the hypothetical 'self. We can't get the real 'self, but we enforce every action taken on the view to be valid for any possible 'self. Note that View<'a> can be invariant over 'a, but this is not a contradiction, because we never actually does any lifetime conversion. (The lifetime appearing in the struct only acts as a placeholder.)

It seems sound to me, so I took some time to implement it. I did not hit the wall of #![feature(fn_traits)] I originally anticipated, but I was troubled by #![feature(closure_lifetime_binder)] instead and I had to workaround it with some tricks. Here's what I got:

My personal favorite is the elegance of into_parts(). It allows you to decouple the owner and the view if and only if the view is no longer dependent on the owner, e.g. after a series of transformation via map(). This is one of the amazing side effects I got with correct typing.

When I first heard about the Japanese dish Oyakodon(親子丼), I thought it was some sort of dark joke. It's serving chicken and egg together with rice in a bowl, so they call it "parent-and-child bowl". What amuses me is that the word "Oyako" in Japanese sounds like a happy family. I bet chickens would not be thrilled about this, but I thought it made for a great name for the crate.

Any feedback would be appreciated!

The first gotcha I checked for is whether Box<T> and &mut T are correctly handled. Glad that’s covered! Though, my understanding is that BowlRef currently has no niche, due to internally using MaybeUninit to get the desired properties. That seems unfortunate, though I suppose that can be improved once std::mem::MaybeDangling is stabilized. My personal solution is to create Box and &mut equivalents which use NonNull, which admittedly loses the possible alignment niches (which the compiler doesn’t currently exploit anyway).

I’m worried about what could happen when T::Target contains non-'static data. Such cases can introduce implied bounds that might mess up your unsafe code’s reasoning about for<> binders. Combined with contravariance, that can cause problems in yoke (which necessitated some additional bounds to prevent this problem). So, I’m concerned about the map method, for instance.

I’m also not convinced by the safety comment of impl AsRef<BowlRef<'b, T, F>> for BowlRef<'a, T, F>. I don’t see why invariance can’t cause unsoundness. I’ll try to explore some edge cases.

I actually found the aliasable crate using the same NonNull solution, too. I like this approach pretty much but it makes the API more complicated.

I would like to hear more about the implied bounds as I figured it's currently an undocumented behavior of HRTB. About AsRef, I'm not claiming that the View<'_> is covariant or contravariant. My proof is to argue that View<'_> can be valid for all lifetimes despite being invariant. And the lifetime used in the struct is merely a placeholder of "all lifetimes" so the major issue is not on AsRef and more on the get()/get_mut() method, I guess.

Thanks! I'll take a look at it.

EDIT: This is a really clever hack. If I'm understanding it correctly, yoke is using whatever lifetime of the reference returned by the constructor. oyakodon might not suffer from this issue because it can distinguish the lifetime returned by the constructor from the lifetime of the owner. So:

use yoke::Yoke;

type F<'a> = fn(&'a ());

fn main() {
    let n = String::from("Hello World!"); // let's call this '1
    let x: Yoke<&'static str, Box<F<'_>>> = Yoke::attach_to_cart(Box::new((|_| {}) as _), |_| &n[..]);
    // infering '_ = '1
    // In oyakodon, `for<'a> F<'a>=&'1 String`
    let y: Yoke<&'static str, Box<F<'static>>> = x;
    let s: &'static Yoke<&'static str, Box<F<'static>>> = Box::leak(Box::new(y));
    let r: &'static str = s.get();
    // here s.get() returns '1 too

    println!("pre-drop: {r}");
    drop(n);
    println!("post-drop: {r}");
}

One of the Views can create an Output for any given lifetime, but that does not imply that any given Output could have been produced from any lifetime.

Counterexample (which triggers UB):

use std::cell::Cell;

use oyakodon::{BowlRef, Derive, View};


struct CellMaker;

impl<'a> View<&'a str> for CellMaker {
    type Output = Cell<&'a str>;
}

impl<'a> Derive<&'a str> for CellMaker {
    fn call(self, input: &'a str) -> Self::Output {
        Cell::new(input)
    }
}

fn main() {
    let bowl = BowlRef::new("Hello", CellMaker);

    {
        let cell = bowl.get();
        cell.set(&"Goodbye".to_owned());
    }

    println!("{}, world!", bowl.get().get());
}

Wow, that was fast! It is a UB. Though I still need some time to figure out why it worked...

I'm currently working on a variance-family crate that makes requirements of covariance (or contravariance, if ever needed) easier to express.

...I lowkey spent all of Wednesday working on a particular issue in the ergonomics of that crate only to give up and declare that Rust's trait solver just cannot perfectly express what I want. So when I say it makes the requirement "easier" to express.... I think that I can move almost all the complexity into variance-family so that its users (usually) get to see sunshine and rainbows, though any edge cases that stray off the happy path require implementing three unsafe traits.

Update on concerns about variance: BowlBox, BowlRef, and BowlMut are each invariant over all their parameters, so concerns about soundness are limited to the explicit cast* methods. No cast methods are capable of changing the owner type, so contravariance of the owner does not matter.

Here's my (failed) attempt to use contravariance + implied bounds to cause unsoundness:

struct StrView<'a>(&'a String);

impl<'a: 'b, 'b> View<&fn(&'b String)> for StrView<'a> {
    type Output = &'b str;
}

impl<'a: 'b, 'b> Derive<&fn(&'b String)> for StrView<'a> {
    fn call(self, _input: &fn(&'b String)) -> Self::Output {
        self.0
    }
}

fn step1<'a>(
    string: &'a String,
) -> BowlRef<'a, Box<fn(&'a String)>, StrView<'a>> {
    BowlRef::new(
        Box::<fn(&'a String)>::new(|_| {}),
        // Oops! `StrView` doesn't implement `Derive<&'c _>` for *all* lifetimes,
        // but thanks to implied bounds, this still works.
        StrView(string),
    )
}

fn step2<'a>(
    bowl: BowlRef<'a, Box<fn(&'a String)>, StrView<'a>>,
) -> BowlRef<'a, Box<fn(&'a String)>, StrView<'static>> {
    // `StrView<'a>` and `StrView<'static>` have the same output for all lifetimes
    // which are at most `'a`, which is good enough thanks to implied bounds.
    bowl.cast_view()
}

fn step3<'a>(
    bowl: BowlRef<'a, Box<fn(&'a String)>, StrView<'static>>
) -> BowlRef<'static, Box<fn(&'static String)>, StrView<'static>> {
    // Since `Box<fn(&'a String)>` can be freely coerced into `Box<fn(&'static String)>`,
    // it sure would be nice if I could map the owner type like that.
    // However, your type is invariant over the owner type and exposes no way to change the
    // owner type.
    // This step is impossible, and is the reason that contravariance + implied bounds
    // do not manage to break your type.
    unimplemented!()
}

I slept over with it and figured that you are totally right about this. get()/get_mut() breaks the HRTB invariant and the view should not be accessed via these methods. I created a safer version, but it's much less ergonomic...

EDIT: I renamed the API.

I found your post about this :smirking_face:

It resembles the yoke issue you mentioned?

Exactly, I tried to translate that yoke problem into your crate’s types.

Life would be much easier if only I had some of your primitives like Varying when writing my crate. If I read it correctly, the problem of variance-family (other than soundness) is that the trait solver cannot prove complex tautologies about lifetimes and so users don't get happy times for free? I hit these issues when writing impl PartialEq and had to trial-and-error finding a way to hint the compiler.

Check the dev branch; I think I finally have what I need, though some of the bounds involved are gnarly:

(And yes, I also got those bounds by trial-and-error.)

Now I'm just fleshing out macros and std implementations for variance-family. Then I'll move on to aliasable-view.

Wish you luck with that. This is often the phase where rustc tells me that my brilliant idea does not work.

I fixed all the tests and am ready for another round of soundness challenge!

This is, apparently, the phase where I make a 500-line macro_rules! token tree muncher in the name of "reducing unnecessary repetition in my code".

2 Likes

@robofinch I was inspired by your variance-family crate about the organization of lifetime families and managed to prove that if there is a proper CovariantFamily trait, covariance-based self-referential structs can be constructed within the model of oyakodon:

This shows that oyakodon is stronger in terms of expressiveness. And yes, consequently, weaker in soundness. But I hope that provides more insights on what are the necessary conditions for building a self-referential struct. I suspect that HRTB style handlers are needed unless we know the lifetime for sure, otherwise an adversarial can always use the opposite of the length of the lifetime we guessed.

I was also reminded by your code that type View<'a>=&'lb &'a &'ub(); has two-sided constraints on 'a. Previously, my code only handled the upper bound. I tried to incoporate the lower bound but some code just stopped working and I'm not sure whether it's a limitation of the trait solver or a bug in my types. I stared at them for hours and I want to take a break away from it now. Thank you very much for your work on variance! I learned a lot from it!

EDIT: allowing a lower bound on the lifetime is unsound. Let fn derive=|s: &'self String| { cell.store(&'lb *s) } then later drop(bowl) and cell.get(). The cell would contain dangling pointer to a String. Seems that letting derive() skip handling any possible lifetime of owner is unsound.

1 Like