-
Notifications
You must be signed in to change notification settings - Fork 544
Add React #191
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add React #191
Changes from all commits
427bda5
8735dfc
a47d0f1
00c2101
eff2501
34fd901
b284baf
ccda60d
b257e7e
f0d4de3
3a64f1f
18b1a76
b4996d9
6e7b392
2602332
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[package] | ||
name = "react" | ||
version = "0.0.0" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
use std::collections::HashMap; | ||
|
||
pub type CellID = usize; | ||
pub type CallbackID = usize; | ||
|
||
struct Cell<'a, T: Copy> { | ||
value: T, | ||
last_value: T, | ||
dependents: Vec<CellID>, | ||
cell_type: CellType<'a, T>, | ||
callbacks_issued: usize, | ||
callbacks: HashMap<CallbackID, Box<FnMut(T) -> () + 'a>>, | ||
} | ||
|
||
enum CellType<'a, T: Copy> { | ||
Input, | ||
Compute(Vec<CellID>, Box<Fn(&[T]) -> T + 'a>), | ||
} | ||
|
||
impl <'a, T: Copy> Cell<'a, T> { | ||
fn new(initial: T, cell_type: CellType<'a, T>) -> Self { | ||
Cell { | ||
value: initial, | ||
last_value: initial, | ||
dependents: Vec::new(), | ||
cell_type: cell_type, | ||
callbacks_issued: 0, | ||
callbacks: HashMap::new(), | ||
} | ||
} | ||
} | ||
|
||
pub struct Reactor<'a, T: Copy> { | ||
cells: Vec<Cell<'a, T>>, | ||
} | ||
|
||
impl <'a, T: Copy + PartialEq> Reactor<'a, T> { | ||
pub fn new() -> Self { | ||
Reactor{ | ||
cells: Vec::new(), | ||
} | ||
} | ||
|
||
pub fn create_input(&mut self, initial: T) -> CellID { | ||
self.cells.push(Cell::new(initial, CellType::Input)); | ||
self.cells.len() - 1 | ||
} | ||
|
||
pub fn create_compute<F: Fn(&[T]) -> T + 'a>(&mut self, dependencies: &[CellID], compute_func: F) -> Result<CellID, &'static str> { | ||
let new_id = self.cells.len(); | ||
for &id in dependencies { | ||
match self.cells.get_mut(id) { | ||
Some(c) => c.dependents.push(new_id), | ||
None => return Err("Nonexistent input"), | ||
} | ||
} | ||
let inputs: Vec<_> = dependencies.iter().map(|&id| self.value(id).unwrap()).collect(); | ||
let initial = compute_func(&inputs); | ||
self.cells.push(Cell::new(initial, CellType::Compute(dependencies.iter().cloned().collect(), Box::new(compute_func)))); | ||
Ok(new_id) | ||
} | ||
|
||
pub fn value(&self, id: CellID) -> Option<T> { | ||
self.cells.get(id).map(|c| c.value) | ||
} | ||
|
||
pub fn set_value(&mut self, id: CellID, new_value: T) -> Result<(), &'static str> { | ||
match self.cells.get_mut(id) { | ||
Some(c) => match c.cell_type { | ||
CellType::Input => { | ||
c.value = new_value; | ||
Ok(c.dependents.clone()) | ||
}, | ||
CellType::Compute(_, _) => Err("Can't set compute cell value directly"), | ||
}, | ||
None => Err("Can't set nonexistent cell"), | ||
}.map(|deps| { | ||
for &d in deps.iter() { | ||
self.update_dependent(d); | ||
} | ||
// We can only fire callbacks after all dependents have updated. | ||
// So we can't combine this for loop with the one above! | ||
for d in deps { | ||
self.fire_callbacks(d); | ||
} | ||
}) | ||
} | ||
|
||
pub fn add_callback<F: FnMut(T) -> () + 'a>(&mut self, id: CellID, callback: F) -> Result<CallbackID, &'static str> { | ||
match self.cells.get_mut(id) { | ||
Some(c) => { | ||
c.callbacks_issued += 1; | ||
c.callbacks.insert(c.callbacks_issued, Box::new(callback)); | ||
Ok(c.callbacks_issued) | ||
}, | ||
None => Err("Can't add callback to nonexistent cell"), | ||
} | ||
} | ||
|
||
pub fn remove_callback(&mut self, cell: CellID, callback: CallbackID) -> Result<(), &'static str> { | ||
match self.cells.get_mut(cell) { | ||
Some(c) => match c.callbacks.remove(&callback) { | ||
Some(_) => Ok(()), | ||
None => Err("Can't remove nonexistent callback"), | ||
}, | ||
None => Err("Can't remove callback from nonexistent cell"), | ||
} | ||
} | ||
|
||
fn update_dependent(&mut self, id: CellID) { | ||
let (new_value, dependents) = { | ||
// This block limits the scope of the self.cells borrow. | ||
// This is necessary becaue we borrow it mutably below. | ||
let (dependencies, f, dependents) = match self.cells.get(id) { | ||
Some(c) => match c.cell_type { | ||
CellType::Input => panic!("Input cell can't be a dependent"), | ||
CellType::Compute(ref dependencies, ref f) => (dependencies, f, c.dependents.clone()), | ||
}, | ||
None => panic!("Cell to update disappeared while querying"), | ||
}; | ||
let inputs: Vec<_> = dependencies.iter().map(|&id| self.value(id).unwrap()).collect(); | ||
(f(&inputs), dependents) | ||
}; | ||
|
||
match self.cells.get_mut(id) { | ||
Some(c) => { | ||
if c.value == new_value { | ||
// No change here, we don't need to update our dependents. | ||
// (It wouldn't hurt to, but it would be unnecessary work) | ||
return; | ||
} | ||
c.value = new_value; | ||
}, | ||
None => panic!("Cell to update disappeared while updating"), | ||
} | ||
|
||
for d in dependents { | ||
self.update_dependent(d); | ||
} | ||
} | ||
|
||
fn fire_callbacks(&mut self, id: CellID) { | ||
let dependents = match self.cells.get_mut(id) { | ||
Some(c) => { | ||
if c.value == c.last_value { | ||
// Value hasn't changed since last callback fire. | ||
// We thus shouldn't fire the callbacks. | ||
return | ||
} | ||
for cb in c.callbacks.values_mut() { | ||
cb(c.value); | ||
} | ||
c.last_value = c.value; | ||
c.dependents.clone() | ||
}, | ||
None => panic!("Callback cell disappeared"), | ||
}; | ||
|
||
for d in dependents { | ||
self.fire_callbacks(d); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
#[allow(unused_variables)] | ||
|
||
// Because these are passed without & to some functions, | ||
// it will probably be necessary for these two types to be Copy. | ||
pub type CellID = (); | ||
pub type CallbackID = (); | ||
|
||
pub struct Reactor<T> { | ||
// Just so that the compiler doesn't complain about an unused type parameter. | ||
// You probably want to delete this field. | ||
dummy: T, | ||
} | ||
|
||
// You are guaranteed that Reactor will only be tested against types that are Copy + PartialEq. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. open question: Can the requirement of Copy be removed? It was very difficult for me to see how. Thinking in terms of what would have to change in the example impl. I don't have rustc to help me verify any of this right now though. I'll verify in a few hours. If T is not
If T is not
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At the very least, Clone is more general than Copy (https://doc.rust-lang.org/std/clone/trait.Clone.html, and see how you must be Clone to Copy: https://doc.rust-lang.org/std/marker/trait.Copy.html), so perhaps the Copy can be relaxed to Clone? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not necessarily what we want, because clone might be expensive and copy not, as https://doc.rust-lang.org/std/clone/trait.Clone.html describes. So maybe Copy is what I want. The difference is currently academic. We only test against usize right now. I would like to not worry about this too much. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Illegal.
Of course. Since There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK in that case I don't care enough. I'm leaving it as |
||
impl <T: Copy + PartialEq> Reactor<T> { | ||
pub fn new() -> Self { | ||
unimplemented!() | ||
} | ||
|
||
// Creates an input cell with the specified initial value, returning its ID. | ||
pub fn create_input(&mut self, initial: T) -> CellID { | ||
unimplemented!() | ||
} | ||
|
||
// Creates a compute cell with the specified dependencies and compute function. | ||
// The compute function is expected to take in its arguments in the same order as specified in | ||
// `dependencies`. | ||
// You do not need to reject compute functions that expect more arguments than there are | ||
// dependencies (how would you check for this, anyway?). | ||
// | ||
// Return an Err (and you can change the error type) if any dependency doesn't exist. | ||
// | ||
// Notice that there is no way to *remove* a cell. | ||
// This means that you may assume, without checking, that if the dependencies exist at creation | ||
// time they will continue to exist as long as the Reactor exists. | ||
pub fn create_compute<F: Fn(&[T]) -> T>(&mut self, dependencies: &[CellID], compute_func: F) -> Result<CellID, ()> { | ||
unimplemented!() | ||
} | ||
|
||
// Retrieves the current value of the cell, or None if the cell does not exist. | ||
// | ||
// You may wonder whether it is possible to implement `get(&self, id: CellID) -> Option<&Cell>` | ||
// and have a `value(&self)` method on `Cell`. | ||
// | ||
// It turns out this introduces a significant amount of extra complexity to this exercise. | ||
// We chose not to cover this here, since this exercise is probably enough work as-is. | ||
pub fn value(&self, id: CellID) -> Option<T> { | ||
unimplemented!() | ||
} | ||
|
||
// Sets the value of the specified input cell. | ||
// | ||
// Return an Err (and you can change the error type) if the cell does not exist, or the | ||
// specified cell is a compute cell, since compute cells cannot have their values directly set. | ||
// | ||
// Similarly, you may wonder about `get_mut(&mut self, id: CellID) -> Option<&mut Cell>`, with | ||
// a `set_value(&mut self, new_value: T)` method on `Cell`. | ||
// | ||
// As before, that turned out to add too much extra complexity. | ||
pub fn set_value(&mut self, id: CellID, new_value: T) -> Result<(), ()> { | ||
unimplemented!() | ||
} | ||
|
||
// Adds a callback to the specified compute cell. | ||
// | ||
// Return an Err (and you can change the error type) if the cell does not exist. | ||
// | ||
// Callbacks on input cells will not be tested. | ||
// | ||
// The semantics of callbacks (as will be tested): | ||
// For a single set_value call, each compute cell's callbacks should each be called: | ||
// * Zero times if the compute cell's value did not change as a result of the set_value call. | ||
// * Exactly once if the compute cell's value changed as a result of the set_value call. | ||
// The value passed to the callback should be the final value of the compute cell after the | ||
// set_value call. | ||
pub fn add_callback<F: FnMut(T) -> ()>(&mut self, id: CellID, callback: F) -> Result<CallbackID, ()> { | ||
unimplemented!() | ||
} | ||
|
||
// Removes the specified callback, using an ID returned from add_callback. | ||
// | ||
// Return an Err (and you can change the error type) if either the cell or callback | ||
// does not exist. | ||
// | ||
// A removed callback should no longer be called. | ||
pub fn remove_callback(&mut self, cell: CellID, callback: CallbackID) -> Result<(), ()> { | ||
unimplemented!() | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unsafe. If this function gets called with a valid dependency followed by an invalid one, an entry is added to the valid cell's dependents, and then the dependent cell is not created. We have to check all cells' validity first.