this post was submitted on 01 Aug 2025
43 points (97.8% liked)

Rust

7507 readers
10 users here now

Welcome to the Rust community! This is a place to discuss about the Rust programming language.

Wormhole

!performance@programming.dev

Credits

  • The icon is a modified version of the official rust logo (changing the colors to a gradient and black background)

founded 2 years ago
MODERATORS
 

So I've been writing an allocator. A difficult task and in Rust it is currently a dream to write.

The patterns I'm on about are small quick scopes to get a mutable reference and store or read data.

pub fn some_func(&mut self, s: &[u8]) {
    // Mutable borrow 1 and write
    {
        let writable = &mut self.buf[..8];

        writable[..8].copy_from_slice();
    }

    // Mutable borrow 2 and write
    {
        let writable = &mut self.buf[8..16];

        writable[8..16].copy_from_slice();
    }

    // And so on . . .
}

No other language feels like this. No other language is so precise on reads and writes always taking a length or the length can be obtained from the type (such as a slice).

Writing to different parts of a buffer and selecting parts of like this feels amazing. For writing an allocator i can just make array's and then write any bytes to them and just read them back and cast them about.

So much better than just using raw pointers, and safer as sizes are usually know with slices.

Anyway i just love Rust and this was an excuse to share my love of Rust <3

you are viewing a single comment's thread
view the rest of the comments
[–] BlackRoseAmongThorns@slrpnk.net 12 points 3 months ago (1 children)

If possible, I'm sure this community would love reading a blog post about exactly this.

Myself, I'm in uni and don't really have time to make new projects in rust and would greatly appreciate to read what you have to say about this :)

[–] fluffy_hub@programming.dev 2 points 3 months ago* (last edited 3 months ago) (1 children)

Here is an update. I focused on the Node which is the smallest element of the buffer, it is what contains any meta data and the data payload. The code is heavily reduced but what is shared is pretty much verbatim maybe some minor edits

Update on the Node for my allocator

[–] BlackRoseAmongThorns@slrpnk.net 2 points 3 months ago (1 children)

I find it really charming how i can sort of see what you're doing even though I'm a complete beginner in this language and we're talking about allocators :^).

That said, the update is greatly appreciated.

[–] fluffy_hub@programming.dev 1 points 3 months ago* (last edited 3 months ago) (1 children)

Ah, got it—let me clarify a bit.

Right now, I’m working with arrays and slicing them into smaller segments for indexing. Technically, I could rely on bit manipulation to generate much of this, but I’ve chosen to lean into Rust’s type system for that extra layer of expressive control.

In Rust, a slice is essentially a fat pointer: it includes both a memory address and a length. Because the slice type encapsulates both, I don’t need to manually track sizes or offsets. Plus, in debug builds, Rust automatically panics if I try to access an out-of-bounds index, which helps catch issues early. And I can use methods like get or get_mut for safe, optional access—either retrieving a value or returning None.

Ultimately, Rust's type system saves me from fiddling with raw bit arithmetic and length bookkeeping. It handles validation elegantly, letting me focus on logic instead of low-level guards.

PS: Although it isn't finished just yet, I'm linking this Node up with a Buf and Slab struct currently. If i remember when I've completed the design and I'm happy to freely distribute the code, I'll make a paste bin and post it here for you. Always happy to help if I can :)

[–] BlackRoseAmongThorns@slrpnk.net 1 points 3 months ago

That clears it up, thank you very much :)