pub struct Receiver<T> { /* private fields */ }
Expand description
A typed receiver.
Implementations§
Source§impl<T: Serialize + DeserializeOwned> Receiver<T>
impl<T: Serialize + DeserializeOwned> Receiver<T>
Sourcepub fn connect<P: AsRef<Path>>(p: P) -> Result<Receiver<T>>
pub fn connect<P: AsRef<Path>>(p: P) -> Result<Receiver<T>>
Connects a receiver to a named unix socket.
Examples found in repository?
examples/simple.rs (line 16)
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 17)
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 into_raw_receiver(self) -> RawReceiver
pub fn into_raw_receiver(self) -> RawReceiver
Converts the typed receiver into a raw one.
Sourcepub fn try_recv(&self) -> Result<Option<T>>
pub fn try_recv(&self) -> Result<Option<T>>
Receives a structured message from the socket if there is a message available.
Sourcepub fn recv(&self) -> Result<T>
pub fn recv(&self) -> Result<T>
Receives a structured message from the socket.
Examples found in repository?
examples/simple.rs (line 18)
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 19)
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<'de, T: Serialize + DeserializeOwned> Deserialize<'de> for Receiver<T>
impl<'de, T: Serialize + DeserializeOwned> Deserialize<'de> for Receiver<T>
Source§fn deserialize<D>(deserializer: D) -> Result<Receiver<T>, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Receiver<T>, D::Error>where
D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Source§impl<T: Serialize + DeserializeOwned> From<RawReceiver> for Receiver<T>
impl<T: Serialize + DeserializeOwned> From<RawReceiver> for Receiver<T>
Source§fn from(value: RawReceiver) -> Self
fn from(value: RawReceiver) -> Self
Converts to this type from the input type.
Source§impl<T: Serialize + DeserializeOwned> FromRawFd for Receiver<T>
impl<T: Serialize + DeserializeOwned> FromRawFd for Receiver<T>
Source§unsafe fn from_raw_fd(fd: RawFd) -> Self
unsafe fn from_raw_fd(fd: RawFd) -> Self
Constructs a new instance of
Self
from the given raw file
descriptor. Read moreAuto Trait Implementations§
impl<T> !Freeze for Receiver<T>
impl<T> RefUnwindSafe for Receiver<T>where
T: RefUnwindSafe,
impl<T> Send for Receiver<T>where
T: Send,
impl<T> Sync for Receiver<T>where
T: Sync,
impl<T> Unpin for Receiver<T>where
T: Unpin,
impl<T> UnwindSafe for Receiver<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