"Interior mutability is a design pattern in Rust that allows you to mutate data even when there are immutable references to that data"
"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:
- Two structures:
- `Workers` that will have two fields:
- `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.
- `states` that will save a state of multiple threads.
If the thread is not dropped, the state will be false otherwise true.
If the thread is not dropped, the state will be false otherwise true.
- `Thread` that will have the following fields:
- `Thread` that will have the following fields:
- `pid`, the id of the thread
- `pid`, the id of the thread.
- `cmd`, the name 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)
- `parent`, that will be the link to the structure `Workers` (Tip: this must be a reference to the structure Workers)
- Implementation of each structure:
- Implementation of each structure:
- `Workers` :
- `Workers` :
- `new`, that creates a default worker
- `new`, that creates a default worker
- `new_worker`, that returns a tuple with the `pid` and a new `Thread`,
- `new_worker`, that returns a tuple with the `pid` and a new `Thread`,
this function must receive a `String` being the `cmd`
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
- 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.
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 for this module is to limit how many times a value is referenced.
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
```rust
fn warning(&self, msg: &str);
fn warning(&self, msg: &str);
@ -17,7 +17,7 @@ For the module you must create the following:
fn error(&self, msg: &str);
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.
`max` being the max count of references the actual value can achieve.
- An implementation of three functions that are associated to the `Tracker` structure:
- An implementation of three functions that are associated to the `Tracker` structure:
@ -27,66 +27,71 @@ 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
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
- `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:
Afterwards you must use the module `messenger` and create the following:
- A structure `Worker` that has the fields:
- A structure `Worker` that has the fields:
- `track_value` this will be the value that will be tracked by the tracker.
- `track_value` this will be the value that will be tracked by the tracker.
- `mapped_messages` that will have the latest messages. This must be a HashMap with the key being the type of message
- `mapped_messages` that will have the latest messages. This must be a HashMap with the key being the type of message
sent by the logger (info, error or warning) and the value being the message
sent by the logger (info, error or warning) and the value being the message
- `all_messages` that will be a vector of all messages sent.
- `all_messages` that will be a vector of all messages sent.
- A `new` function that initializes the structure `Worker`
- A `new` function that initializes the structure `Worker`
- 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
- 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.
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)