mirror of https://github.com/01-edu/public.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Zewasik
3726c9a3b0
|
2 years ago | |
---|---|---|
.. | ||
README.md | 2 years ago |
README.md
drop_the_thread
Instructions
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, you will create a Drop Checker API.
Define the following structures:
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 befalse
, and will betrue
otherwise.
Thread
: containing:pid
: the id of the thread.cmd
: the name of the thread.parent
: a link to the structureWorkers
. (Tip: this should be a reference).
You'll need to also add the following associated functions to the structures:
-
Workers
:new
: that creates a default worker.new_worker
: that returns a tuple with thepid
and a newThread
. This function must receive aString
representing thecmd
.is_dropped
: that receives apid
and returns abool
that indicates the state of the thread.track_worker
: which returns ausize
representing the length of thestates
vector. (The index of the next new thread).add_drop
: which is called by theDrop
trait. It will receive apid
that will be used to change the state of the thread. If the state of that thread istrue
then it will panic with the message"X is already dropped"
, whereX
represents thepid
). Otherwise it should change the state totrue
and increment thedrops
field by 1.
-
Thread
:new_thread
: that initializes a new thread.skill
: that drops the thread.
-
You must implement the
Drop
trait for theThread
structure. In this trait you must call the functionadd_drop
so that the state of the thread changes.
Expected Functions
use std::cell::{RefCell, Cell};
#[derive(Debug, Default, Clone, Eq, PartialEq)]
pub struct Workers {
pub drops: Cell<usize>,
pub states: RefCell<Vec<bool>>
}
impl Workers {
pub fn new() -> Workers {}
pub fn new_worker(&self, c: String) -> (usize, Thread) {}
pub fn track_worker(&self) -> usize {}
pub fn is_dropped(&self, id: usize) -> bool {}
pub fn add_drop(&self, id: usize) {}
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Thread<'a> {
// expected public fields
}
impl<'a> Thread<'a> {
pub fn new_thread(p: usize, c: String, t: &'a Workers) -> Thread {}
pub fn skill(self) {}
}
Usage
Here is a program to test your function,
use std::rc::Rc;
use drop_the_thread::*;
fn main() {
let worker = Workers::new();
let (id, thread) = worker.new_worker(String::from("command"));
let (id1, thread1) = worker.new_worker(String::from("command1"));
thread.skill();
println!("{:?}", (worker.is_dropped(id), id, &worker.drops));
thread1.skill();
println!("{:?}", (worker.is_dropped(id1), id1, &worker.drops));
let (id2, thread2) = worker.new_worker(String::from("command2"));
let thread2 = Rc::new(thread2);
let thread2_clone = thread2.clone();
drop(thread2_clone);
println!("{:?}", (worker.is_dropped(id2), id2, &worker.drops, Rc::strong_count(&thread2)));
}
And its output:
$ cargo run
(true, 0, Cell { value: 1 })
(true, 1, Cell { value: 2 })
(false, 2, Cell { value: 2 }, 1)
$