Tech News
← Back to articles

Placing Functions

read original related products more articles

placing functions

— 2025-07-08

What are placing functions?

About a year ago I observed that in-place construction seems surprisingly simple. By separating the creating of the place in memory from writing the value to memory, it’s not that hard to see how we can turn that into a language feature. So about six months ago, that’s what I went ahead and did and created the placing crate: a proc-macro-based prototype for “placing functions”.

Placing functions are functions whose return type is constructed in the caller’s stack frame rather than in the function’s stack frame. This means that the address from the moment on construction is stable. Which may not only lead to improved performance, it also serves as the foundation for a number of useful features like supporting dyn AFITs (Async Functions in Traits) .

In this post I’ll be explaining how placing functions desugar, why placing functions are the right solution for emplacement, and how placing functions integrate into the language. Not in as much detail as I’d do for an actual RFC, but more as a broad introduction to the idea of placing functions. And to get right into it, here is a basic example of how this would work:

struct Cat { age : u8 , } impl Cat { #[ placing ] // ← Marks a function as "placing". fn new ( age : u8 ) -> Self { Self { age } // ← Constructs `Self` in the caller's frame. } fn age (& self ) -> & u8 { & self .age } } fn main () { let cat = Cat::new( 12 ); // ← `Cat` is constructed in-place. assert_eq!(cat. age (), & 12 ); }

A basic desugaring

The purpose of the placing crate is to prove that placing functions should not be that hard to implement. I managed to implement a working prototype in a few hours over my winter holidays. In total I've spent maybe four or so days on the implementation. I’m not a compiler engineer though, and I expect that the lovely folks on T-Compiler can probably recreate this in a fraction of the time it took me.

Since I don't know my way around the rustc frontend, I implemented the placing crate entirely using proc macros. The upside was that I could get something working more quickly. The downside is that proc macros don’t have access to type information, so I had to hack around that limitation. Which results in an API that requires a lot of proc macro attributes. But that’s ok for a proof of concept.

... continue reading