How we migrate our framework into async/await



Firstly, congratulation on Rust lang achieving stable async/await syntax! As of the release, async/await is becoming the preferred way to do asynchronous programming instead of using Futures in Rust lang.

In Obsidian Web Framework, we do the same move just like other libraries which enabling async/await syntax in order to provide a better development experience.

*Rust 1.40 is used in this article

In case you don’t know async/await in Rust

InfoQ Journey to Async/Await

Why async/await?

Code complexity — You may design the code in the synchronous way.

Readability — Avoid and_then and then chaining which making code hard to read.

Lifetime — Lifetime is following synchronous flow within async function.

In Obsidian, we treat the developer’s development experience as the first priority goal. Thus, migration to async/await enabled structure is definitely needed.

Changing futures to async/await

The process is pretty simple for most of the cases. Basically, we just add async to the function and remove then ,and_then and the dangerous wait to await. From the example below, it shows that the code is much cleaner and readable after using async/await.


old file load function


new file load function

Using async/await in trait and closure

In current Rust version, the async syntax does not support trait function and closure yet. It will be supported in the future. So, we need to do it another way around which is like this.

async in trait and closure

It seems complicated for a trait but luckily Rust community create a crate to simplify this which is called as async-trait. By using this crate, we are able to use traits with asyncsyntax!

using async_trait crate

You may notice that this trait workaround method will result in a heap allocation per-function-call. So why do we use it? In Obsidian, we aim for delivering a better development experience. We believe that enabling await for traits like Middleware ease the development process. Besides that, we also trust the Rust community will enable the use of async in trait soon. 😀

For more information on the trade-off, visit the Asynchronous Programming in Rust book.

How to test an async API?

Sadly, now test function cannot use async syntax. Thus, we need to make it synchronous. In our framework, we use async-std task blocking feature to make it happen.

async in test

Lifetime issue

Although async/await seems perfect, it can be hard when dealing with self or struct outside of async block lifetime. In some cases, the developer might want to call an async implementation within the same struct. In this case, the compiler will not let you go. For instance:

async lifetime issue using clone

The simplest way will be simply cloning the struct before async block call which is used above. However, the system will have the scalability issue. In Obsidian, the clone method decreased performance by ~25% with the sample of 1 endpoint router and 20 endpoint router. So, we decided to go for another approach by moving out the usage of self into synchronous part and move its ownership into the async block for the async process.

async lifetime issue without clone

About Obsidian Framework

Obsidian is a web development framework built in Rust which vision to lower down the learning curve of web development in Rust without losing the advantage of it. Currently, Obsidian is under active development and we expected to release v 0.2 in this couple of months.




A software developer. Feel free to visit my personal website

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Introducing Gitlab in our DevSecOps Journey

Introducing Gitlab in our DevSecOps Journey


Designing a 24-bit VGA Adapter

How To Automatically Quarantine Security Groups if ALL TRAFFIC mentioned and send Notification to…

Projection Metrics — The magic that happens when you know your capacity

Cross platform mobile development Part III — iOS

Software: Fast and slow

15 Useful Slack Tips You Should Know

Slack customize slack

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Pai Lee Wai

Pai Lee Wai

A software developer. Feel free to visit my personal website

More from Medium

Try them all

Car Wash Pattern: Parallelizing Non-Thread Safe and/or CPU-intensive Processes with Future Based…

Compile Clojure to native binary using GraalVM

Daily Native Dev: Rust & Sentry story