Guard pattern

The guard pattern is a design pattern to restrict access to a value of a certain type. The value can only be accessed through a guard, which is essentially a wrapper struct. It usually implements the trait to do something when the guard is destroyed.

An example is the type from the standard library. The inner value can only be accessed by calling the  method. This returns a, which implements the trait, so fields and methods of the inner value can be easily accessed. The  method locks the mutex, and when the   is dropped, the mutex is unlocked:

Since values are automatically dropped when they go out of scope, it's usually not necessary to call explicitly. This also ensures that a mutex is never locked without unlocking it afterwards:

Example
TODO