cluExtIO/
lib.rs

1//Copyright 2018 #UlinProject Денис Котляров
2
3//Licensed under the Apache License, Version 2.0 (the "License");
4//you may not use this file except in compliance with the License.
5//You may obtain a copy of the License at
6
7//       http://www.apache.org/licenses/LICENSE-2.0
8
9//Unless required by applicable law or agreed to in writing, software
10//distributed under the License is distributed on an "AS IS" BASIS,
11//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//See the License for the specific language governing permissions and
13// limitations under the License.
14
15
16//#Ulin Project 1718
17//
18
19/*!
20Syntactic sugar extends I/O capabilities.
21
22# Capabilities:
231. EmptyWrite - Empty "Write" that does nothing.
242. UnionWrite - Possibility to combine several "Write" into one record.
253. MutexWrite - Combining Mutex and Write for multi-threaded access.
264. ExtWrite - The trait extends the capabilities of the standard Write, adds lock methods.
275. FlushDropWrite - An implementation of "Trait Write", which calls the flush () method on drop. 
286. FlushLockWrite - An implementation of "Trait Write" that calls the flush() method when removing a lock.
297. NotChanWrite - Unchangeable "Trait Write".
30...
31
32
33# Use
34
351. UnionWrite
36```
37extern crate cluExtIO;
38
39use std::io::Write;
40use cluExtIO::UnionWriteConst;
41use std::fs::File;
42
43pub fn main() {
44
45     let file1 = File::create("/tmp/1.out").unwrap();
46     //file1 - `Write trait`
47
48     let file2 = File::create("/tmp/2.out").unwrap();
49     //file2 - `Write trait`
50
51     let write = file1.union(file2);
52     //UnionWrite - `FILE1+FILE2`
53
54
55     my_function(write).unwrap();
56}
57
58fn my_function<W: Write>(mut w: W) -> Result<(), std::io::Error> {
59     w.write_fmt(format_args!("#@{} {}\n", 1, "Test"))?;
60     w.write_fmt(format_args!("#@{} {}\n", 2, "MyString"))?;
61     w.write_fmt(format_args!("#@{} {}\n", 3, "MyString"))?;
62
63     Ok( () )
64}
65```
66
672. StdLock
68```
69extern crate cluExtIO;
70
71use cluExtIO::ExtWrite;
72use std::io::Write;
73
74pub fn main() {
75
76     let out = std::io::stdout();
77
78     my_function(&out, 0, "No eND:)").unwrap();
79     
80     out.lock_fn(|mut l| {
81          l.write(b"End.\n")
82     }).unwrap();
83}
84
85fn my_function<'a, W: ExtWrite<'a>>(raw_write: &'a W, n: usize, str: &'static str) -> Result<(), std::io::Error> {
86     let mut lock = raw_write.lock();
87
88     lock.write_fmt(format_args!("#@{} {}\n", n, "Test"))?;
89     lock.write_fmt(format_args!("#@{} {}\n", n+1, "MyString"))?;
90     lock.write_fmt(format_args!("#@{} ~{}\n", n+2, str))?;
91
92     Ok( () )
93}
94```
95
963. Threads
97```
98extern crate cluExtIO;
99
100use std::io::stdout;
101use cluExtIO::UnionWriteConst;
102use cluExtIO::ExtWrite;
103use cluExtIO::MutexWrite;
104use cluExtIO::FlushLockWrite;
105use cluExtIO::NotChanWrite;
106use std::io::Write;
107use std::fs::File;
108use std::sync::Arc;
109use std::sync::Barrier;
110use std::thread;
111
112pub fn main() {
113     let arc_out = Arc::new({       
114          let out = stdout();
115
116          let file = FlushLockWrite::new(MutexWrite::new(File::create("/tmp/file.out").unwrap()));
117          //Contains the implementation of ExtWrite. Safe for inter-thread space.
118          //+ Additional self-cleaning after destroying Lock
119
120          let file2 = FlushLockWrite::new(MutexWrite::new(File::create("/tmp/file2.out").unwrap()));
121          //Contains the implementation of ExtWrite. Safe for inter-thread space.
122          //+ Additional self-cleaning after destroying Lock
123          
124          out.union(file).union(file2)
125     }); //Combined `ExtWrite` with lock function. OUT_PIPE + FILE_PIPE(2) = UNION_SAFE_PIPE
126
127
128     let barrier = Arc::new(Barrier::new(5 + 1));
129
130     for num_thread in 0..5 {
131          let barrier = barrier.clone();
132          let arc_out = arc_out.clone();
133          thread::spawn(move || {
134
135               arc_out.lock_fn(|mut lock| {
136                    lock.write_fmt(format_args!("#@{} {}\n", num_thread, "Thread #OK")).unwrap();
137                    lock.write_fmt(format_args!("#@{} {}\n", num_thread, "Thread #T")).unwrap();
138               });
139
140               barrier.wait();
141          });
142     }
143
144     barrier.wait();
145
146     arc_out.write_fmt(format_args!("#@{} {}\n", 999, "Thread pull end.")).unwrap();
147     //Arc<UnionWrite>, auto lock methods.
148
149     // /tmp/file.out+
150     // /tmp/file.out+
151}
152```
153
154
155
156# License
157
158Copyright 2018 #UlinProject Денис Котляров
159
160Licensed under the Apache License, Version 2.0
161
162*/
163
164mod write;
165pub use self::write::*;
166
167