Browse Source

update ref_cell

pull/1285/head
davidrobert99 2 years ago committed by David Mata
parent
commit
3d7ff3c1d3
  1. 12
      subjects/ref_cell/README.md

12
subjects/ref_cell/README.md

@ -18,31 +18,35 @@ fn error(&self, msg: &str);
```
Implement the `Tracker` structure with the following fields:
- `logger`: a reference to `Logger`.
- `value`: the count of how many times the value was referenced. It should not exceed `max`.
- `max`: the max count of references.
Add the following associated functions to `Tracker`:
- `new`: that initializes the structure.
- `set_value`: that sets the `value`. It should compare the number of references to `value` and `max` to work out the percentage used. It should write to the following traits if it exceeds the specified usage percentage:
- percentage >= 100%: `"Error: you are over your quota!"` should be written to `error`.
- percentage >= 70% and percentage < 100%: `"Warning: you have used up over X% of your quota! Proceeds with precaution"` should be written to `warning`, where `X` should be replaced with the calculated percentage.
- `peek`: that will take a peek at how much usage the variable already has. It should write `"Info: you are using up too X% of your quote"` to the `info` trait function. `X` should be replaced with the calculated percentage.
- `peek`: that will take a peek at how much usage the variable already has. It should write `"Info: you are using up to X% of your quota"` to the `info` trait function. `X` should be replaced with the calculated percentage.
### Second part (lib.rs)
Now that you've created `messenger`, you can now create the following:
Create the `Worker` structure with the following fields:
- `track_value`: which is the value that will be tracked by the tracker.
- `mapped_messages`: that will store the latest messages from the `Logger` trait functions. This will be a HashMap. The key will represent the type of message (`info`, `error` or `warning`), and the value will be the actual message.
- `all_messages`: that will be a vector of **all** messages sent.
Create the following associated functions for `Worker`:
- `new`: that initializes a `Worker` structure.
- `Logger`: 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 field of the `Worker` structure.
You must use **interior mutability**, this means it must be possible 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.
You must use **interior mutability**, this means it must be possible 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.
### Usage
@ -96,11 +100,11 @@ And its output:
```console
$ cargo run
("Info", "you are using up too 40% of your quote")
("Info", "you are using up to 40% of your quota")
("Warning", "you have used up over 90% of your quota! Proceeds with precaution")
("Error", "you are over your quota!")
[
"Info: you are using up too 40% of your quote",
"Info: you are using up to 40% of your quota",
"Warning: you have used up over 80% of your quota! Proceeds with precaution",
"Warning: you have used up over 90% of your quota! Proceeds with precaution",
"Error: you are over your quota!"

Loading…
Cancel
Save