These are chat archives for rust-lang/rust

19th
Apr 2018
Bryan Burgers
@bryanburgers
Apr 19 2018 01:15

@patientplatypus You need to create a registry first. You want something more like:

let registry = Registry::new(); // Create the registry
let observable = registry.new_observable(...); // Use the registry

https://play.rust-lang.org/?gist=a0f21e7063d1318f72ff8f351385ffe6&version=stable

Registry::new() is a convention for creating a new thing. You don't need it. You could also instantiate it in your main like
let registry = Registry {
  observables: Vec::new(),
  observers: Vec::new(),
}; // Create the registry
let observable = registry.new_observable(...);
patientplatypus
@patientplatypus
Apr 19 2018 01:19
@bryanburgers this is what i have so far
v
Bryan Burgers
@bryanburgers
Apr 19 2018 01:20
:thumbsup: Keep at it!
patientplatypus
@patientplatypus
Apr 19 2018 01:48
why does observers: Vec::new() work but value: Box::new() give the error expected 1 parameter
?
Dylan DPC
@Dylan-DPC
Apr 19 2018 07:36
@patientplatypus because Box accepts 1 parameter
Denis Lisov
@tanriol
Apr 19 2018 09:01
@patientplatypus I'd suggest that looping over all observers when an event happens on any observable is not very scalable.
You may want to have some structure in the registry that allows you to enumerate only the observers that should be notified.
What I want to do here is be able to have the messages update the values in Observer and have the values be saved in the Observer array, but because I'm piping them through messages it doesnt do that.
Any advice?
Denis Lisov
@tanriol
Apr 19 2018 13:03
@patientplatypus Looks like you're sharing the same old version...
patientplatypus
@patientplatypus
Apr 19 2018 13:07
@tanriol ah - sorry. You apparently have to hit the share button again in order to save your progress. Try this: https://play.rust-lang.org/?gist=8648f443f3c66bb308a10025470e5f09&version=stable
Denis Lisov
@tanriol
Apr 19 2018 13:15
The API seems weird... what, again, are you trying to do?
patientplatypus
@patientplatypus
Apr 19 2018 13:47
@tanriol - basically i just want a message passing service so that I have a named observable passing messages to named observers. So if I have observable "YOLO" it only passes messages to observers called "YOLO", and if I have an observable "CATS" it only passes messages to observers called "CATS". I hope that makes sense.
Bryan Burgers
@bryanburgers
Apr 19 2018 13:50
@patientplatypus I don't want to overload you with too much at once, but depending on how you want to use this observable/observer system, you may eventually want to look into std::sync::mpsc as a way of passing messages across threads.
Another question you may run into eventually, is this: do you want to send the same type of message every time? Or are there certain types of messages that are generated by YOLO observables, and other types of messages that are generated by CATS observables?
E.g., do YOLO observables always generate "Deploy On Friday Afternoon" messages, and CATS observables always generate "Sing Memories" message? Because you may want to eventually use Rust's type system to make sure that a CATS observer never receives a "Deploy On Friday Afternoon" message.
Denis Lisov
@tanriol
Apr 19 2018 13:53
How do you expect the observers to react to changes? Just a callback or something different?
Dylan DPC
@Dylan-DPC
Apr 19 2018 14:00
is there a function that tells which index an element exists in a vector? scanned the vector docs twice and haven't found anything helpful
patientplatypus
@patientplatypus
Apr 19 2018 14:00
@tanriol so atm all I want to do is be able to pass strings to different observables on the same thread. I've seen mpsc but for a first run at it I just want to get this simple use case working
Bryan Burgers
@bryanburgers
Apr 19 2018 14:06
@patientplatypus Totally fair. Small steps to get started. Refine the approach as you learn.
Dylan DPC
@Dylan-DPC
Apr 19 2018 14:21

is there a function that tells which index an element exists in a vector? scanned the vector docs twice and haven't found anything helpful

Anyone? :P

Michal 'vorner' Vaner
@vorner
Apr 19 2018 14:23
Isn't there something like find?
(Likely on an iterator, not Vec itself)
Dylan DPC
@Dylan-DPC
Apr 19 2018 14:27
yeah was looking at the same article but was hoping there is more "direct way". looking at find now
Dylan DPC
@Dylan-DPC
Apr 19 2018 14:51
i was trying binary_search but it doesn't work on a vector of strings (i assume it expects the input to be sorted like normal binary search)
got it working with position. Thanks all :)