Struct Bootstrapper

Source
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>

Source

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
Hide additional 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}
Source

pub fn bind<P: AsRef<Path>>(p: P) -> Result<Bootstrapper<T>>

Creates a bootstrapper at a specific socket path.

Source

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
Hide additional 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}
Source

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
Hide additional 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>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Drop for Bootstrapper<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V