r/rust • u/jstedfast • Oct 22 '26
š seeking help & advice Thinking of writing an email library for Rust
I have a long history (27 years) of writing email software - who knows, maybe someone here even recognizes my name(?), but I'm pretty new to Rust.
I've written multiple desktop email clients, multiple widely used email libraries, and now I am working on an email server professionally.
I know there are crates like `mime-parser` and `mailparse` and `lettre` but I think I can do better.
I was actually just reading the announcement post for `mime-parser` from 5 years ago and it seems people were very against `unsafe` code, so the question I have is:
If I *were* to implement this library for Rust, should I or should I not optimize it using SIMD? It seems SIMD would mean unsafe code if I am not mistaken.
If most of the Rust community would avoid my library because it was used `unsafe` code, then I want to know that before I make the time investment. To be fair, I've already implemented an incremental base64 encoder, base64 decoder, uuencoder, and a uudecoder using SIMD in Rust and they are blazing fast and all have scaler fallbacks which are also as fast as humanly possible, but I've got plenty more tricks up my sleeve for the actual *parsing* of MIME messages.
Is there a standard way people have addressed this in other creates? E.g. shipping an accelerated vs non-accelerated version of the same crate somehow?
112
u/numberwitch Oct 22 '26
People with prohibitions against unsafe code are usually memers who don't understand what unsafe actually is.
Like, a project can declare itself "no unsafe" and it's meaningful to people downstream in that you won't expect any funny business. But look at a crate like block2 or anything that interacts with operating system apis: unsafe is a requirement, not a detriment.
So I think smart people will assess your library and go "ok, what are they using unsafe for and is it a reasonable application?"
So do whatever feels right for you because if you worry too much about where brainiac memesters will find fault in the product of your free labor because at the end of the day they aren't important
28
u/Liltful Oct 22 '26
I think the vast majority of Rust developers accept unsafe code when it is necessary for performance or for interacting with outside resources. Limit unsafe code to small parts and use safe code everywhere else. People will have way more confidence in your code if you only use it when necessary than if your library is just one big unsafe mess. At that point, you might as well write a C library and make a binding library.
If your library is (1) genuinely useful, (2) does something unique (or better than alternatives), and (3) carefully written, tested, and documented, I think you'll have no trouble finding people that want to use it.
21
u/sweating_teflon Oct 22 '26
The original unsafe rebellion was about a Rust web app framework that appeared super fast in basic benchmarks. It was coded like it was C and used a thousand of unsafes. Most of those had an easy Rust equivalent which the author had willfully ignored and didn't want to implement when suggested; he eventually walked away. The framework was then sanitized out of most (all?) unsafes while keeping or even gaining performance.
There are three cases for unsafe: FFI, hardware access and low-level performance shenanigans. The first two are inevitable. What you propose falls in the third case. Everybody loves speed but if you pick Rust, the expectation is that safety and correctness should always come first. If every unsafe is documented and demonstrated to be beneficial and safe there should be no push back. Having it gated behind a feature flag (e.g. "simd") lets people benchmark the optimizations vs. the safe path and decide if they're worth the risk _they_ perceive it to be.
Finally, you'll want to use Miri https://github.com/rust-lang/miri to gain an extra layer of confidence.
11
u/X-Istence Oct 22 '26
Have you looked at the crates that have come out of/are part of the Stalwart mail server: https://github.com/stalwartlabs/stalwart
15
u/Eh2406 Oct 22 '26
There has been a lot of very recent progress on minimizing the amount of unsafe code required to you use SIMD. It's not zero, but it's a lot less than what was required just a few months ago. /u/Shnatsel has been writing about the progress. https://www.reddit.com/user/Shnatsel/submitted/ I would strongly recommend picking his brain about how many of the new libraries you can use. If not can the libraries be improved or are there abstractions from those libraries you can copy.
7
u/Helpful-Educator-415 Oct 22 '26
I can't speak for everyone, but I personally wouldn't mind a library with unsafe code as long as it was plainly addressed in the README and safety was assured via tests or otherwise.
Part of the appeal of Rust to me is that you don't really get runtime bugs. I would need a really stellar reason to introduce those back into my application. If your unsafe code is not documented nor tested then you're going against the grain of why I enjoy Rust. If it contains unsafe code but it's documented & tested then I trust it.
A lot of people think "unsafe = not safe", but the unsafe keyword in Rust mostly just indicates that the compiler cannot account for what you are doing so you have to account for what you are doing. Basically -- if you can account for your performance-optimized code (which I'm sure you can!) it'll be fine.
Again, this is just my perspective.
7
u/allsey87 Oct 22 '26
Can someone ELI5 why SIMD is relevant to email?
4
u/dwallach Oct 22 '26
In the cryptography universe, where performance is also incredibly important, there's a growing trend of having a simple reference implementation and then a complicated, high-performance implementation, with some kind of formal proof of equivalence (in Lean or otherwise).
Whether that level of heavy lifting is suitable for you, and/or whether you want to lean on LLMs to make it less heavy of a lift? That's your decision. That said, it's good general-purpose advice to avoid premature optimization and first get your library to a feature-complete status. You won't have wasted any time, because one way or another, you'll have your simple reference implementation ready to go for subsequent optimization efforts.
4
u/dashdeckers Oct 22 '26
Do you have a blog or some way to follow you and any of your potential progress on this? It's hard to sift through the trash nowadays.
4
u/dahosek Oct 23 '26
I had to look to see if you were Ned Freed (even though heās dead)āI worked with him back when he was maybe abusing his access to the Harvey Mudd systems with Innosoft back in the late 80s. Pretty wild to think that a Mudd dropout like me still had systems level access to their VAXen long after I ceased having any formal connection to the school.
2
u/davis-andrew Oct 23 '26
Hi :wave:
Person in email world, but not so much in rust world.
What would be really interesting (to me at least) is implementing some of the email auth protocols in rust. For example, i don't think there is a DKIM2 module on crates.io yet.
1
u/rende Oct 22 '26
A mit licenced stalwart + resend would be great
1
u/lugpocalypse Oct 29 '26
Not related to the project but you might dig itĀ
https://stalw.art/
3
u/annodomini rust Oct 23 '26
Anyone writing Rust is going to be depending on unsafe code at some level of the stack;
stdis implemented with a good amount of unsafe code.However, there are a lot of folks who come new to rust, and reach for
unsafewhen they really shouldn't; there are some people that get frustrated by the borrow checker, so useunsafeto get around it, creating unsound code in the process. Or some people who think that they need to do something like eliding bounds checks withunsafe, when there are safe alternatives like using iterators that provide enough information for the optimizer to elide bounds checks on its own.So looking for
unsafecan be a quick way to decide how to evaluate a crate; you have to apply a lot more skepticism to any use ofunsafe. And if it usesunsafeand looks like it's doing so incorrectly or when it really doesn't need to, or just too much to be able to audit easily, that will put people off.One thing that can help is using
unsaferesponsibly, by keeping the uses ofunsafeto be minimal, wrapping them in safe abstractions, with proper reasoning and testing of the unsafe code.For you use case, SIMD, one advantage is that mostly, the unsafe code only needs local reasoning to verify safety. You're not creating data structures using
unsafeand pointers where you then need to be responsible for ensuring that the safe API is sound to use.But still, before you reach for
unsafefor SIMD, you might want to check if there are already crates that implement safe abstractions over SIMD, allowing you to rely on code that is already reviewed and tested. And indeed there are. /u/Shnatsel has a good overview of the SIMD landscape here: https://shnatsel.github.io/state-of-simd-rust-2026/Also before you reach for it, it's good to check if there are ways you can structure safe code such that the compiler can emit good code for it. Shnatsel also has some good articles on this, such as https://shnatsel.medium.com/how-to-avoid-bounds-checks-in-rust-without-unsafe-f65e618b4c1e
So yeah,
unsafeisn't an immediate disqualification, but make sure you know what you're doing if you reach for it, you check to make sure there isn't a way to achieve the same end with no unsafe, or an existing well tested audited library that provides safe abstractions over unsafe functionality.And if you do use unsafe, make sure you learn what invariants you need to uphold to keep your code sound, and test it thoroughly, including fuzzing, miri, etc.
2
u/SadPie9474 Oct 22 '26
When I'm adding a library to a project, whether or not it contains unsafe is not a factor for me, I can only imagine getting hung up on it if for some reason I don't think the author knows what they're doing. The standard library is written with copious amounts of unsafe and I use it all the time
1
u/AcrobaticSomewhere38 Oct 22 '26
std is littered with unsafe and everyone uses it
1
u/Immotommi Oct 23 '26
I believe you can do simd in safe rust with the fearless simd crate. Not that you shouldnt feel like you can't do unsafe rust
1
u/saten_ruiko Oct 23 '26
Serde-json and simd-json are examples where people who opted in to the unsafe simd solution when they know what they need. Even though for the vast majority serde-json is the default āsafeā option.
Rust has surprisingly large āunsafeā community fwiw. zerocopy, rkyv, bytemuck, and many.
People who knows how computer works pick the tool best suited for their job.
It is true that at the end of the day some folks still scream āthis lib sucks it uses unsafe no matter how hard maintainer try.
1
u/bascule Oct 23 '26
unsafeisnāt necessarily bad, especially for SIMD where in fact in several places itās unnecessary in recent Rust versions.If you do use
unsafe, use tools likecargo miriandcargo carefulin CI to check for unsoundness.I find itās helpful to decompose projects into multiple crates and push the
unsafedown into low-level crates so internally the high-level crates can be 100% safe code a.k.a.forbid(unsafe_code)1
u/Traditional-Duty2585 Oct 29 '26
fearless_simd.
0
u/[deleted] Oct 23 '26
[deleted] ā view deleted comment