Skip to content

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

Merged
merged 15 commits into from
Sep 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
"parallel-letter-frequency",
"rectangles",
"forth",
"circular-buffer"
"circular-buffer",
"react"
],
"deprecated": [

Expand Down
4 changes: 4 additions & 0 deletions exercises/react/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions exercises/react/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[package]
name = "react"
version = "0.0.0"
163 changes: 163 additions & 0 deletions exercises/react/example.rs
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),
Copy link
Member Author

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.

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);
}
}
}
89 changes: 89 additions & 0 deletions exercises/react/src/lib.rs
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.
Copy link
Member Author

Choose a reason for hiding this comment

The 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 Copy and create_input is made to pass an &T:

  • then the best Cell can do is to also store the &T
  • But then, if the compute functions return T, how will that be dealt with? Perhaps https://doc.rust-lang.org/std/borrow/enum.Cow.html ? But that means T has to be ToOwned. Still, maybe that's better
  • On the other hand if the compute functions return &T... but how is that possible? Where can they store a T that will live long enough? I don't think that's possible.

If T is not Copy and create_input still passes a T:

  • I think the cell can take ownership for value, and a reference for last_value.
  • I don't understand how that can work - when value gets replaced, what keeps last_value alive?
  • I guess I'll see what happens when I try it.
  • Callbacks and compute functions have to take &T, that should be no problem.

Copy link
Member Author

Choose a reason for hiding this comment

The 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?

Copy link
Member Author

Choose a reason for hiding this comment

The 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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If T is not Copy and create_input still passes a T:

  • I think the cell can take ownership for value, and a reference for last_value.

Illegal.

src/lib.rs:23:26: 23:33 error: `initial` does not live long enough
src/lib.rs:23             last_value: &initial,
                                       ^~~~~~~
src/lib.rs:21:60: 30:6 note: reference must be valid for the lifetime 'a as defined on the block at 21:59...
src/lib.rs:21     fn new(initial: T, cell_type: CellType<'a, T>) -> Self {
                                                                         ^
src/lib.rs:21:60: 30:6 note: ...but borrowed value is only valid for the scope of function body at 21:59
src/lib.rs:21     fn new(initial: T, cell_type: CellType<'a, T>) -> Self {

Of course. Since initial is the parameter to that function. Doh.

Copy link
Member Author

Choose a reason for hiding this comment

The 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 Copy.

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!()
}
}
Loading