1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use crate::Pipe;
use lazy_static::lazy_static;
use std::{io::Write, sync::Mutex};
use dashmap::DashMap;
lazy_static!
{
static ref PIPES: DashMap<String, Mutex<Pipe>> = DashMap::new();
}
#[macro_export]
macro_rules! pprint
{
($name:tt, $($arg:tt)*) => ($crate::print($name, format!($($arg)*).as_str()));
}
#[macro_export]
macro_rules! pprintln
{
($name:tt) => ($crate::print($name, "\n"));
($name:tt, $($arg:tt)*) => ($crate::print($name, format!($($arg)*).as_str()))
}
pub fn init(name: &str) -> crate::Result<Pipe>
{
let pipe = Pipe::with_name(name)?;
let reader = pipe.clone();
PIPES.insert(name.to_string(), Mutex::from(pipe));
Ok(reader)
}
pub fn get(name: &str) -> Option<Pipe>
{
PIPES.get(name).map(|pipe| pipe.lock().unwrap().clone())
}
pub fn close(name: &str)
{
match PIPES.remove(name)
{
Some((_, pipe)) => { drop(pipe.lock().unwrap()) }
None => {}
}
}
pub fn close_all()
{
PIPES.clear()
}
#[inline]
pub fn print(name: &str, s: &str) -> crate::Result<usize>
{
match PIPES.get(name)
{
None => Err(crate::Error::Ipipe("Pipe not initialized")),
Some(pipe) =>
{
let mut pipe = pipe.lock()?;
match pipe.write(s.as_bytes())
{
Ok(written) => Ok(written),
Err(e) => Err(crate::Error::from(e))
}
}
}
}