"Interior mutability is a design pattern in Rust that allows you to mutate data even when there are immutable references to that data"
You must create a Drop checker API. For this you must create:
in this exercise a Drop checker API has to be created. For this you must define:
- Two structures:
- `Workers` that will have two fields:
- `drops` that will save the number of dropped threads
- `drops` that will save the number of dropped threads.
- `states` that will save a state of multiple threads.
If the thread is not dropped, the state will be false otherwise true.
- `Thread` that will have the following fields:
- `pid`, the id of the thread
- `cmd`, the name of the thread
- `pid`, the id of the thread.
- `cmd`, the name of the thread.
- `parent`, that will be the link to the structure `Workers` (Tip: this must be a reference to the structure Workers)
- Implementation of each structure:
- `Workers` :
- `new`, that creates a default worker
- `new_worker`, that returns a tuple with the `pid` and a new `Thread`,
this function must receive a `String` being the `cmd`
@ -33,7 +36,13 @@ You must create a Drop checker API. For this you must create:
- You must implement for the structure `Thread` the `Drop` trait. In this trait you must call the function `add_drop` so that the state of the thread changes
Create a module called `messenger`. This module will be able to inform a user of how much references of a given value he is using.
The main objective for this module is to limit how many times a value is referenced.
Create a module called `messenger`. This module will be able to inform a user of how many references of a given value he is using.
The main objective of this module is to limit how many times a value is referenced.
For the module you must create the following:
For this module the following must be created:
- A trait `Logger`that implements three functions: `warning`, `info`, `error`. All function should receive a reference to themselves and a string literal.
- A trait `Logger`which implements three functions: `warning`, `info`, `error`. All **functions** should receive a reference to themselves and a string literal.
```rust
fn warning(&self, msg: &str);
@ -17,7 +17,7 @@ For the module you must create the following:
fn error(&self, msg: &str);
```
- A structure called `Tracker`, that must have the fields: `logger` being a reference to the `Logger`, `value` being the count of how many times the value was referenced,
- A structure called `Tracker`, which must have the fields: `logger` being a reference to the `Logger`, `value` being the count of how many times the value was referenced,
`max` being the max count of references the actual value can achieve.
- An implementation of three functions that are associated to the `Tracker` structure:
@ -27,7 +27,7 @@ For the module you must create the following:
If the percentage is equal or greater to 70% of the limit usage, it should write **("Warning: you have used up over {}% of your quota! Proceeds with precaution", <calculated_percentage>)** to the `warning` function
- `peek` that will take a peek of how much usage the variable already has. It should write **("Info: you are using up too {}% of your quote", <calculated_percentage>)** to the `info` function
### 2ª part
### Second part (lib.rs)
Afterwards you must use the module `messenger` and create the following:
@ -40,53 +40,58 @@ Afterwards you must use the module `messenger` and create the following:
- To use the trait `Logger` you must implement it for the Worker structure. Each function (warning, error and info) must insert the message to the
respective fields of the structure Worker.
You must use **interior mutability**, this means it must be able to mutate data even when there are immutable references to that data.
You must use **interior mutability**, this means it must be able to mutate data even when there are immutable references to that data. Consequently, the user will not need to use the keyword `mut` (tip: RefCell)
So the user doesn't need to use the keyword `mut` (tip: RefCell)