Rust: Singleton Application. Borrow checker vs design patterns | by Ángel Berhó | May, 2022

Borrow checker vs design patterns

Hello everybody. Let’s discuss certainly one of Rust’s ideas that make Rust builders go loopy: the Borrow checker.

Foto de Michael Dziedzic en Unsplash

However first, let me clarify what that is and why you need to be on this matter if you wish to begin to develop in Rust.

In different compiled languages, akin to C++, you have to management reminiscence, i.e., allocate and free dynamic reminiscence. In others like Java, you don’t face this drawback as a result of a rubbish collector is operating and listening for unused reminiscence to free it within the background.

Rust makes it “less complicated” or makes it appear less complicated, as a result of when a scope is terminated, Rust frees it. Additionally, once you go the worth to a perform/technique (one other scope), this scope will get the variable property, and when its scope terminates, this reminiscence is freed once more. Let’s see an instance:

Due to this fact, if we need to reuse some_string in these capabilities, we now have to change to get a reference as a substitute of the variable’s worth within the print perform.

Or return the worth once more to not free it.

There are different guidelines with the borrow checker that may increase an error. For instance, when passing a reference to a struct constructor, the borrow checker will increase an error as a result of it doesn’t understand how lengthy the struct or the variable you go as a reference can stay, so the compiler will power you to make use of lifetime. However, beware, this can be a entice.

Borrow checker is the guard to keep away from reminiscence leaks

We will say the borrow checker is the guard to keep away from reminiscence leaks that make you debug for hours to search out out the occasional bug.

There are some design patterns, akin to Singleton, that the borrow checker may hinder. It’s because, within the Singleton technique, you have to retailer an occasion of the construction as static within the construction.

Singleton is accountable for storing a worldwide occasion of itself and being reused by all the utility to retailer international variables akin to configurations or connections to a service.

In idea, the singleton works like this. We create a category/struct with an attribute to itself and a way that creates an occasion of itself and returns it.

Spoiler: Rust doesn’t allow you to create this for 2 causes:

  • You’ll be able to’t use an attribute from a struct with out instantiating.
  • The borrow checker gained’t allow you to retailer an occasion of itself as a result of it detects it as a reminiscence leak.

That’s why Rust core builders design some instruments for this.

This construction lets you share references between completely different domains by way of a weak reference. How does it work?

This makes the code compile as a result of the Rc construction creates a weak reference as an attribute that tells the compiler that it doesn’t must examine at compile time. The issue is we aren’t utilizing this attribute after creating it. So, within the subsequent name, it’s going to create it once more.

Nonetheless, we are able to use this software to construction knowledge as a dynamic record, however not as a Singleton.

This solely works in an area thread. When you want extra threads, I like to recommend you employ Arc. This trait implements the Clone trait to clone references with threadsafe.

Mutex

It would retailer our price as mutable however will block the useful resource till somebody leaves the reference to this worth. For instance, if we now have a variable of kind u8, it’s going to make it mutable despite the fact that it’s a primitive kind.

So, why is it nonetheless not getting the precise worth? Nicely, within the get_instance we’re instantiating the Singleton construction every time we name it and never utilizing our precise occasion attribute. So, we now have to get from any international state.

What’s the issue? Static variables can’t be mutable as a result of Rust believes you possibly can create race circumstances, and so it forces you to make use of unsafe scope.

thread_local!

This macro permits the creation of static variables with out a non-constant worth therapy. Right here’s an instance:

#[derive(Default)]

This derive makes our struct implement the Default trait. The trait implements the first and non-primary default values of Rust. So, we don’t have to fret about defaults now.

thread_local! 
static SINGLETON_POOL: Arc<Singleton> = Arc::new(Default::default());

Making a static default worth of our Singleton. As well as, we now have modified Rc to Arc (Atomic Reference Depend), which permits us to clone the reference.

pub fn get_instance() -> Arc<Singleton>  singleton_pool.clone())

The get_instance technique now returns a worldwide state of the Singleton. And voilà, we bought our singleton working.

It isn’t a good suggestion to create a Singleton as different languages do. Maybe it might be less complicated to retailer the variable within the father or mother state and share it among the many parts that want this configuration. For instance, the Actix net framework implements a state that shall be handed in every request.

Thanks for studying; I hope you prefer it! I’d love to listen to your ideas and experiences working with design patterns or Rust.

Depart a remark or contact me on LinkedIn.

More Posts