pub struct R2PipeThread {
pub id: u16,
pub handle: JoinHandle<Result<()>>,
/* private fields */
}Expand description
Stores thread metadata It stores both a sending and receiving end to the thread, allowing convenient interaction So we can send commands using R2PipeThread::send() and fetch outputs using R2PipeThread::recv()
Fields§
§id: u16§handle: JoinHandle<Result<()>>Implementations§
Source§impl R2PipeThread
impl R2PipeThread
Sourcepub fn send(&self, cmd: String) -> Result<()>
pub fn send(&self, cmd: String) -> Result<()>
Examples found in repository?
examples/threads.rs (line 21)
3fn main() -> Result<()> {
4 // Lets spawn some r2pipes to open some binaries
5 // First two arguments for R2Pipe::threads() are the same as for R2Pipe::spawn() but inside vectors
6 // Third and last argument is an option of a callback function
7 let pipes = match R2Pipe::threads(
8 vec!["/bin/ls", "/bin/id", "/bin/cat"],
9 vec![None, None, None],
10 None,
11 ) {
12 Ok(p) => p,
13 Err(e) => {
14 println!("Error spawning Pipes: {}", e);
15 return Ok(());
16 }
17 };
18
19 // At this point we can iter through all of our r2pipes and send some commands
20 for p in pipes.iter() {
21 if let Ok(_) = p.send("ij".to_string()) {};
22 }
23
24 // do_other_stuff_here();
25
26 // Lets iter again and see what the pipes got
27 for p in pipes.iter() {
28 // this will block, do "p.recv(false)" for non-blocking receive inside a loop
29 if let Ok(msg) = p.recv(true) {
30 println!("Pipe #{} says: {}", p.id, msg);
31 }
32 }
33
34 // Finally properly close all pipes
35 // Note: For "join()" we need to borrow so pipes.iter() won't work for this
36 for p in pipes {
37 if let Ok(_) = p.send("q".to_string()) {};
38 p.handle.join().unwrap()?;
39 }
40
41 Ok(())
42}More examples
examples/threads_callback.rs (line 28)
4fn main() -> Result<()> {
5 // First we define a callback. It doesn't block and gets called after a thread receives output from r2pipe
6 // Note: First argument to the callback is the thread id, second one the r2pipe output
7 let callback = Arc::new(|id, result| {
8 println!("Pipe #{} says: {}", id, result);
9 });
10
11 // First two arguments for R2Pipe::threads() are the same as for R2Pipe::spawn() but inside vectors
12 // Third and last argument is an option to a callback function
13 // The callback function takes two Arguments: Thread ID and r2pipe output
14 let pipes = match R2Pipe::threads(
15 vec!["/bin/ls", "/bin/id", "/bin/cat"],
16 vec![None, None, None],
17 Some(callback),
18 ) {
19 Ok(p) => p,
20 Err(e) => {
21 println!("Error spawning Pipes: {}", e);
22 return Ok(());
23 }
24 };
25
26 // At this point we can iter through all of our r2pipes and send some commands
27 for p in pipes.iter() {
28 if let Ok(_) = p.send("ij".to_string()) {};
29 }
30
31 // Meanwhile: Expecting callbacks
32 std::thread::sleep(std::time::Duration::from_millis(1000));
33
34 // Finally properly close all pipes
35 // Note: For "join()" we need to borrow so pipes.iter() won't work for this
36 for p in pipes {
37 if let Ok(_) = p.send("q".to_string()) {};
38 p.handle.join().unwrap()?;
39 }
40
41 Ok(())
42}Sourcepub fn recv(&self, block: bool) -> Result<String>
pub fn recv(&self, block: bool) -> Result<String>
Examples found in repository?
examples/threads.rs (line 29)
3fn main() -> Result<()> {
4 // Lets spawn some r2pipes to open some binaries
5 // First two arguments for R2Pipe::threads() are the same as for R2Pipe::spawn() but inside vectors
6 // Third and last argument is an option of a callback function
7 let pipes = match R2Pipe::threads(
8 vec!["/bin/ls", "/bin/id", "/bin/cat"],
9 vec![None, None, None],
10 None,
11 ) {
12 Ok(p) => p,
13 Err(e) => {
14 println!("Error spawning Pipes: {}", e);
15 return Ok(());
16 }
17 };
18
19 // At this point we can iter through all of our r2pipes and send some commands
20 for p in pipes.iter() {
21 if let Ok(_) = p.send("ij".to_string()) {};
22 }
23
24 // do_other_stuff_here();
25
26 // Lets iter again and see what the pipes got
27 for p in pipes.iter() {
28 // this will block, do "p.recv(false)" for non-blocking receive inside a loop
29 if let Ok(msg) = p.recv(true) {
30 println!("Pipe #{} says: {}", p.id, msg);
31 }
32 }
33
34 // Finally properly close all pipes
35 // Note: For "join()" we need to borrow so pipes.iter() won't work for this
36 for p in pipes {
37 if let Ok(_) = p.send("q".to_string()) {};
38 p.handle.join().unwrap()?;
39 }
40
41 Ok(())
42}Auto Trait Implementations§
impl Freeze for R2PipeThread
impl !RefUnwindSafe for R2PipeThread
impl Send for R2PipeThread
impl !Sync for R2PipeThread
impl Unpin for R2PipeThread
impl !UnwindSafe for R2PipeThread
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