ExtWrite

Trait ExtWrite 

Source
pub trait ExtWrite<'a>: Write {
    type LockWrite: Write + 'a;

    // Required method
    fn lock(&'a self) -> Self::LockWrite;

    // Provided method
    fn lock_fn<F: FnMut(Self::LockWrite) -> R, R>(&'a self, f: F) -> R { ... }
}
Expand description

The trait extends the capabilities of the standard Write.

Required Associated Types§

Required Methods§

Source

fn lock(&'a self) -> Self::LockWrite

Blocking the output stream.

Provided Methods§

Source

fn lock_fn<F: FnMut(Self::LockWrite) -> R, R>(&'a self, f: F) -> R

Alternative method of blocking the output stream using the closure.

Examples found in repository?
examples/std_lock.rs (lines 14-16)
8pub fn main() -> Result<(), Error> {
9
10     let out = std::io::stdout();
11
12     my_function(&out, 0, "No eND:)")?;
13     
14     out.lock_fn(|mut l| {
15          l.write(b"End.\n")
16     })?;
17
18     Ok( () )
19}
More examples
Hide additional examples
examples/maximum.rs (lines 28-30)
11pub fn main() -> Result<(), Error> {
12     let out = {
13          let std_out = std::io::stdout();
14          
15          let file = FlushLockWrite::new(MutexWrite::new(File::create("/tmp/file.out")?));
16          //Contains the implementation of ExtWrite. Safe for inter-thread space.
17          //+ Additional self-cleaning after destroying Lock
18
19          let file2 = FlushLockWrite::new(MutexWrite::new(File::create("/tmp/file2.out")?));
20          //Contains the implementation of ExtWrite. Safe for inter-thread space.
21          //+ Additional self-cleaning after destroying Lock
22          
23          std_out.union(file).union(file2)
24     }; //Combined `ExtWrite` with lock function. OUT_PIPE + FILE_PIPE(2) = UNION_SAFE_PIPE
25
26     my_function(&out, 0, "No eND:)")?;
27     
28     out.lock_fn(|mut l| {
29          l.write(b"End.\n")
30     })?;
31
32     // STDOUT+
33     // /tmp/file.out+
34     // /tmp/file.out+
35
36     Ok( () )
37}
examples/thread.rs (lines 39-42)
16pub fn main() {
17     let arc_out = Arc::new({       
18          let out = stdout();
19
20          let file = FlushLockWrite::new(MutexWrite::new(File::create("/tmp/file.out").unwrap()));
21          //Contains the implementation of ExtWrite. Safe for inter-thread space.
22          //+ Additional self-cleaning after destroying Lock
23
24          let file2 = FlushLockWrite::new(MutexWrite::new(File::create("/tmp/file2.out").unwrap()));
25          //Contains the implementation of ExtWrite. Safe for inter-thread space.
26          //+ Additional self-cleaning after destroying Lock
27          
28          out.union(file).union(file2)
29     }); //Combined `ExtWrite` with lock function. OUT_PIPE + FILE_PIPE(2) = UNION_SAFE_PIPE
30
31
32     let barrier = Arc::new(Barrier::new(5 + 1));
33
34     for num_thread in 0..5 {
35          let barrier = barrier.clone();
36          let arc_out = arc_out.clone();
37          thread::spawn(move || {
38
39               arc_out.lock_fn(|mut lock| {
40                    lock.write_fmt(format_args!("#@{} {}\n", num_thread, "Thread #OK")).unwrap();
41                    lock.write_fmt(format_args!("#@{} {}\n", num_thread, "Thread #T")).unwrap();
42               });
43
44               barrier.wait();
45          });
46     }
47
48     barrier.wait();
49
50     arc_out.write_fmt(format_args!("#@{} {}\n", 999, "Thread pull end.")).unwrap();
51     //Arc<UnionWrite>, auto lock methods.
52
53     // /tmp/file.out+
54     // /tmp/file.out+
55}

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<'a> ExtWrite<'a> for Stderr

Source§

type LockWrite = StderrLock<'a>

Source§

fn lock(&'a self) -> Self::LockWrite

Source§

impl<'a> ExtWrite<'a> for Stdout

Source§

type LockWrite = StdoutLock<'a>

Source§

fn lock(&'a self) -> Self::LockWrite

Source§

impl<'a, 'l, L: ExtWrite<'a, LockWrite = W> + Write, W: 'a + Write> ExtWrite<'a> for &'l mut L
where Self: Write,

The trait extends the capabilities of the standard Write.

Source§

fn lock(&'a self) -> Self::LockWrite

Blocking the output stream.

Source§

fn lock_fn<F: FnMut(Self::LockWrite) -> R, R>(&'a self, f: F) -> R

Alternative method of blocking the output stream using the closure.

Source§

type LockWrite = W

Source§

impl<'a, 'l, L: ExtWrite<'a, LockWrite = W>, W: 'a + Write> ExtWrite<'a> for &'l L
where Self: Write,

The trait extends the capabilities of the standard Write.

Source§

fn lock(&'a self) -> Self::LockWrite

Blocking the output stream.

Source§

fn lock_fn<F: FnMut(Self::LockWrite) -> R, R>(&'a self, f: F) -> R

Alternative method of blocking the output stream using the closure.

Source§

type LockWrite = W

Implementors§

Source§

impl<'a> ExtWrite<'a> for EmptyWrite

Source§

impl<'a, T: 'a + Write> ExtWrite<'a> for MutexWrite<T>

Source§

impl<'a, T: ExtWrite<'a>> ExtWrite<'a> for FlushLockWrite<'a, T>

Source§

impl<'a, W: ExtWrite<'a>, W2: ExtWrite<'a>> ExtWrite<'a> for UnionWrite<W, W2>