January 2019
Intermediate to advanced
520 pages
14h 32m
English
In this user handler, we need access to a database with users. Because the database is a Slab instance that's wrapped with a Mutex instance, we have to lock the mutex to have exclusive access to a slab. There's a Mutex::lock function that returns Result<MutexGuard, PoisonError<MutexGuard>>. MutexGuard is a scoped lock, which means it leaves the code block or scope in, and it implements the Deref and DerefMut traits to provide transparent access to data under the guard object.
It's a good practice to report all errors in the handler. You can log errors and return a 500 (Internal Error) HTTP code to the client. To keep it simple, we'll use an unwrap method and expect the mutex to lock correctly:
let mut users ...
Read now
Unlock full access