diff --git a/subjects/drop_the_thread/README.md b/subjects/drop_the_thread/README.md index 55b11a47..b5664078 100644 --- a/subjects/drop_the_thread/README.md +++ b/subjects/drop_the_thread/README.md @@ -2,45 +2,35 @@ ### Instructions -"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. -in this exercise a Drop checker API has to be created. For this you must define: +In this exercise, you will create a Drop Checker API. -- Two structures: +Define the following structures: - - `Workers` that will have two fields: - - `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. - - `parent`, that will be the link to the structure `Workers` (Tip: this must be a reference to the structure Workers) -- Implementation of each structure:state +- `Workers`: containing: + - `drops`: that will save the number of dropped threads. + - `states`: that will save the state of multiple threads. If the thread is not dropped, the state will be `false`, and will be `true` otherwise. +- `Thread`: containing: + - `pid`: the id of the thread. + - `cmd`: the name of the thread. + - `parent`: a link to the structure `Workers`. (Tip: this should be a reference). - - `Workers` : +You'll need to also add the following associated functions to the structures: - - `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` - - `is_dropped`, that receives a `pid` and returns a `bool` that indicates the state of the thread by using the `pid` - - `track_worker`, it should return a `usize`, that will be the last available index of the `states` vector, being the new next thread - - `add_drop`, this function must be **called by the `Drop` trait**. It will receive a `pid` that will be used to change the - state of the thread. If the state of that thread is `true` then it will panic with the message ("Cannot drop {}, because its already dropped", pid). - Otherwise it should change the state to true and increment the `drops` field by one. +- `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` representing the `cmd`. + - `is_dropped`: that receives a `pid` and returns a `bool` that indicates the state of the thread. + - `track_worker`: which returns a `usize` representing the length of the `states` vector. (The index of the next new thread). + - `add_drop`: which is **called by the `Drop` trait**. It will receive a `pid` that will be used to change the state of the thread. If the state of that thread is `true` then it will panic with the message `"Cannot drop X, because its already dropped"`, where `X` represents the `pid`). Otherwise it should change the state to `true` and increment the `drops` field by 1. - - `Thread`: - - `new_thread`, that initializes a new thread - - `skill`, that drops the thread +- `Thread`: + - `new_thread`: that initializes a new thread. + - `skill`: that drops the thread. -- 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 - -### Notions - -- [Trait std::ops::Drop](https://doc.bccnsoft.com/docs/rust-1.36.0-docs-html/std/ops/trait.Drop.html) -- [Struct std::cell::RefCell](https://doc.rust-lang.org/std/cell/struct.RefCell.html) -- [Interior Mutability](https://doc.rust-lang.org/book/ch15-05-interior-mutability.html) +- You must implement the `Drop` trait for the `Thread` structure. In this trait you must call the function `add_drop` so that the state of the thread changes. ### Expected Functions @@ -111,3 +101,9 @@ $ cargo run (false, 2, Cell { value: 2 }, 1) $ ``` + +### Notions + +- [Trait std::ops::Drop](https://doc.bccnsoft.com/docs/rust-1.36.0-docs-html/std/ops/trait.Drop.html) +- [Struct std::cell::RefCell](https://doc.rust-lang.org/std/cell/struct.RefCell.html) +- [Interior Mutability](https://doc.rust-lang.org/book/ch15-05-interior-mutability.html)