R2PipeThread

Struct R2PipeThread 

Source
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

Source

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

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§

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.