pub struct Bootstrapper<T> { /* private fields */ }
Expand description
A bootstrap helper.
This creates a unix socket that is linked to the file system so
that a Receiver
can connect to it. It
lets you send one or more messages to the connected receiver.
Implementations§
Source§impl<T: Serialize + DeserializeOwned> Bootstrapper<T>
impl<T: Serialize + DeserializeOwned> Bootstrapper<T>
Sourcepub fn new() -> Result<Bootstrapper<T>>
pub fn new() -> Result<Bootstrapper<T>>
Creates a bootstrapper at a random socket in /tmp
.
Examples found in repository?
examples/simple.rs (line 12)
11fn main() {
12 let bootstrapper = Bootstrapper::new().unwrap();
13 let path = bootstrapper.path().to_owned();
14
15 std::thread::spawn(move || {
16 let receiver = Receiver::<Task>::connect(path).unwrap();
17 loop {
18 let task = receiver.recv().unwrap();
19 match task {
20 Task::Sum(values, tx) => {
21 tx.send(values.into_iter().sum::<i64>()).unwrap();
22 }
23 Task::Shutdown => break,
24 }
25 }
26 });
27
28 println!("make channel 1");
29 let (tx, rx) = channel().unwrap();
30 bootstrapper.send(Task::Sum(vec![23, 42], tx)).unwrap();
31 println!("result: {}", rx.recv().unwrap());
32
33 println!("make channel 2");
34 let (tx, rx) = channel().unwrap();
35 bootstrapper.send(Task::Sum(vec![1, 2, 3], tx)).unwrap();
36 println!("result: {}", rx.recv().unwrap());
37
38 bootstrapper.send(Task::Shutdown).unwrap();
39}
More examples
examples/proc.rs (line 28)
15fn main() {
16 if let Ok(path) = env::var(ENV_VAR) {
17 let receiver = Receiver::<Task>::connect(path).unwrap();
18 loop {
19 let task = receiver.recv().unwrap();
20 match dbg!(task) {
21 Task::Sum(values, tx) => {
22 tx.send(values.into_iter().sum::<i64>()).unwrap();
23 }
24 Task::Shutdown => break,
25 }
26 }
27 } else {
28 let bootstrapper = Bootstrapper::new().unwrap();
29 let mut child = process::Command::new(env::current_exe().unwrap())
30 .env(ENV_VAR, bootstrapper.path())
31 .spawn()
32 .unwrap();
33
34 let (tx, rx) = channel().unwrap();
35 bootstrapper.send(Task::Sum(vec![23, 42], tx)).unwrap();
36 println!("result: {}", rx.recv().unwrap());
37
38 let (tx, rx) = channel().unwrap();
39 bootstrapper.send(Task::Sum((0..10).collect(), tx)).unwrap();
40 println!("result: {}", rx.recv().unwrap());
41
42 bootstrapper.send(Task::Shutdown).unwrap();
43
44 child.kill().ok();
45 child.wait().ok();
46 }
47}
Sourcepub fn bind<P: AsRef<Path>>(p: P) -> Result<Bootstrapper<T>>
pub fn bind<P: AsRef<Path>>(p: P) -> Result<Bootstrapper<T>>
Creates a bootstrapper at a specific socket path.
Sourcepub fn path(&self) -> &Path
pub fn path(&self) -> &Path
Returns the path of the socket.
Examples found in repository?
examples/simple.rs (line 13)
11fn main() {
12 let bootstrapper = Bootstrapper::new().unwrap();
13 let path = bootstrapper.path().to_owned();
14
15 std::thread::spawn(move || {
16 let receiver = Receiver::<Task>::connect(path).unwrap();
17 loop {
18 let task = receiver.recv().unwrap();
19 match task {
20 Task::Sum(values, tx) => {
21 tx.send(values.into_iter().sum::<i64>()).unwrap();
22 }
23 Task::Shutdown => break,
24 }
25 }
26 });
27
28 println!("make channel 1");
29 let (tx, rx) = channel().unwrap();
30 bootstrapper.send(Task::Sum(vec![23, 42], tx)).unwrap();
31 println!("result: {}", rx.recv().unwrap());
32
33 println!("make channel 2");
34 let (tx, rx) = channel().unwrap();
35 bootstrapper.send(Task::Sum(vec![1, 2, 3], tx)).unwrap();
36 println!("result: {}", rx.recv().unwrap());
37
38 bootstrapper.send(Task::Shutdown).unwrap();
39}
More examples
examples/proc.rs (line 30)
15fn main() {
16 if let Ok(path) = env::var(ENV_VAR) {
17 let receiver = Receiver::<Task>::connect(path).unwrap();
18 loop {
19 let task = receiver.recv().unwrap();
20 match dbg!(task) {
21 Task::Sum(values, tx) => {
22 tx.send(values.into_iter().sum::<i64>()).unwrap();
23 }
24 Task::Shutdown => break,
25 }
26 }
27 } else {
28 let bootstrapper = Bootstrapper::new().unwrap();
29 let mut child = process::Command::new(env::current_exe().unwrap())
30 .env(ENV_VAR, bootstrapper.path())
31 .spawn()
32 .unwrap();
33
34 let (tx, rx) = channel().unwrap();
35 bootstrapper.send(Task::Sum(vec![23, 42], tx)).unwrap();
36 println!("result: {}", rx.recv().unwrap());
37
38 let (tx, rx) = channel().unwrap();
39 bootstrapper.send(Task::Sum((0..10).collect(), tx)).unwrap();
40 println!("result: {}", rx.recv().unwrap());
41
42 bootstrapper.send(Task::Shutdown).unwrap();
43
44 child.kill().ok();
45 child.wait().ok();
46 }
47}
Sourcepub fn send(&self, val: T) -> Result<()>
pub fn send(&self, val: T) -> Result<()>
Consumes the boostrapper and sends a single value in.
This can be called multiple times to send more than one value into the inner socket.
Examples found in repository?
examples/simple.rs (line 30)
11fn main() {
12 let bootstrapper = Bootstrapper::new().unwrap();
13 let path = bootstrapper.path().to_owned();
14
15 std::thread::spawn(move || {
16 let receiver = Receiver::<Task>::connect(path).unwrap();
17 loop {
18 let task = receiver.recv().unwrap();
19 match task {
20 Task::Sum(values, tx) => {
21 tx.send(values.into_iter().sum::<i64>()).unwrap();
22 }
23 Task::Shutdown => break,
24 }
25 }
26 });
27
28 println!("make channel 1");
29 let (tx, rx) = channel().unwrap();
30 bootstrapper.send(Task::Sum(vec![23, 42], tx)).unwrap();
31 println!("result: {}", rx.recv().unwrap());
32
33 println!("make channel 2");
34 let (tx, rx) = channel().unwrap();
35 bootstrapper.send(Task::Sum(vec![1, 2, 3], tx)).unwrap();
36 println!("result: {}", rx.recv().unwrap());
37
38 bootstrapper.send(Task::Shutdown).unwrap();
39}
More examples
examples/proc.rs (line 35)
15fn main() {
16 if let Ok(path) = env::var(ENV_VAR) {
17 let receiver = Receiver::<Task>::connect(path).unwrap();
18 loop {
19 let task = receiver.recv().unwrap();
20 match dbg!(task) {
21 Task::Sum(values, tx) => {
22 tx.send(values.into_iter().sum::<i64>()).unwrap();
23 }
24 Task::Shutdown => break,
25 }
26 }
27 } else {
28 let bootstrapper = Bootstrapper::new().unwrap();
29 let mut child = process::Command::new(env::current_exe().unwrap())
30 .env(ENV_VAR, bootstrapper.path())
31 .spawn()
32 .unwrap();
33
34 let (tx, rx) = channel().unwrap();
35 bootstrapper.send(Task::Sum(vec![23, 42], tx)).unwrap();
36 println!("result: {}", rx.recv().unwrap());
37
38 let (tx, rx) = channel().unwrap();
39 bootstrapper.send(Task::Sum((0..10).collect(), tx)).unwrap();
40 println!("result: {}", rx.recv().unwrap());
41
42 bootstrapper.send(Task::Shutdown).unwrap();
43
44 child.kill().ok();
45 child.wait().ok();
46 }
47}
Trait Implementations§
Source§impl<T: Debug> Debug for Bootstrapper<T>
impl<T: Debug> Debug for Bootstrapper<T>
Auto Trait Implementations§
impl<T> !Freeze for Bootstrapper<T>
impl<T> !RefUnwindSafe for Bootstrapper<T>
impl<T> Send for Bootstrapper<T>where
T: Send,
impl<T> !Sync for Bootstrapper<T>
impl<T> Unpin for Bootstrapper<T>where
T: Unpin,
impl<T> UnwindSafe for Bootstrapper<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more