Struct pipedconsole::Console

source ·
pub struct Console {
    pub pid: u32,
    /* private fields */
}
Expand description

Used for printing and reading from an external console.

This struct is the main interaction point to a worker process you can controll using this struct’s methods like Self::println or everything else documented here.

Examples

use pipedconsole::Console;
let my_console = Console::new("My console")?;
 
// Prints hello world on another console window.
my_console.println("What is your name?");
 
let mut name = String::new();
my_console.read_to_string(&mut name)?
 
// Prints normally on the calling processe's console.
println!("Your name is: {}", name);

Cloning

You can clone this struct however you want, but note that all cloned instances will controll the same console window.

Threads

A Console cannot be send between threads safely, as that would create raceconditions with the worker process.

Fields§

§pid: u32

The process id of the worker process. You can use this to further interface with the process. You can shutdown the worker process using this, to ensure that it is closed “correctly” even if it get’s stuck, although that souldn’t happen under normal conditions.

Implementations§

source§

impl Console

source

pub fn new(name: &str) -> Result<Self, Error>

Creates a new Console object with the specified name.

This function is currently the only way of launching a new console. It spawns a worker process wich waits for any messages from the parent and then prints them. For more information about that see [console-worker].

The console is closed automaticly when the returned Console is dropped or your program exits.

Examples
use pipedconsole::Console;
let my_console = Console::new("My console")?; // creates a new console window
 
my_console.println("Hello world!")?;
Technical details

This method creates a worker process using the CreateProcess function from winapi and then obtains a handle to the pipe by calling the CreateFile function. For more information about the information in returned errors see crate::Error: pipedconsole::Error .

source§

impl Console

source

pub fn print<T: ToString>(&self, message: T) -> Result<usize, Error>

Print to the extern console.

To guarantee that the console is flushed one may call the the flush function when done printing.

Examples
use pipedconsole::Console;
let my_console = Console::new()?;
 
// Produces "Hello world!" as an output.
my_console.print("Hello ")?;
my_console.print("world!")?;
my_console.flush()?;
source§

impl Console

source

pub fn println<T: ToString>(&self, message: T) -> Result<usize, ConsoleError>

Print a line to the extern console.

This method appends a newline and then calls print: Console::print with that message.

Examples
use pipedconsole::Console;
let my_console = Console::new()?;
 
// Prints hello world on another window, no "\n" needed.
my_console.println("Hello world!")?;
source§

impl Console

source

pub fn read_line(&self, target: &mut String) -> Result<usize, Error>

Read a line from the console. Similar to std::io::stdin().read_to_string()

Reads from the console until a newline (the 0xA byte) is reached, and appends them to the provided buffer. This function will block until all the input was read.

Examples
use pipedconsole::Console;
let my_console = Console::new()?;
let mut buffer = String::new();
 
my_console.read_line(&mut buffer)?;
println!("You entered {}", &buffer);
 

Trait Implementations§

source§

impl Clone for Console

source§

fn clone(&self) -> Console

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Console

source§

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

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

impl Drop for Console

Closes the handle to the pipe. When the handle is closed, the worker process will automaticly exit.

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl Write for Console

source§

fn write(&mut self, buf: &[u8]) -> Result<usize>

Prints the data to the console. The data must be valid utf-8, otherwise ErrorKind::InvalidInput will be returned. If an internal error is generated, ErrorKind::InvalidData may be retuend.

source§

fn flush(&mut self) -> Result<()>

Force the console to flush.

This function should be called when you are done printing something, to ensure that is actually gets displayed correctly.

Usually this function only needs to be called after a call to crate::Console::print. (If no newline character is at the end of the message.)

Examples
use pipedconsole::Console;
let my_console = Console::new("My Console")?;
for i 0..100 {
    my_console.print(i)?;
};
 
my_console.flush()?;
1.36.0 · source§

fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> Result<usize, Error>

Like write, except that it writes from a slice of buffers. Read more
source§

fn is_write_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Writer has an efficient write_vectored implementation. Read more
1.0.0 · source§

fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>

Attempts to write an entire buffer into this writer. Read more
source§

fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>

🔬This is a nightly-only experimental API. (write_all_vectored)
Attempts to write multiple buffers into this writer. Read more
1.0.0 · source§

fn write_fmt(&mut self, fmt: Arguments<'_>) -> Result<(), Error>

Writes a formatted string into this writer, returning any error encountered. Read more
1.0.0 · source§

fn by_ref(&mut self) -> &mut Selfwhere
    Self: Sized,

Creates a “by reference” adapter for this instance of Write. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere
    T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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> ToOwned for Twhere
    T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

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

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.