Skip to main content

network_communicator/
config.rs

1/// Structure for configuration.
2#[derive(Debug,Clone)]
3pub struct Config {
4	thread_count: usize,
5	limit_result_channel_buffer: usize,
6	limit_task_channel_buffer: usize,
7}
8
9quick_error! {
10	/// Errors in configuration.
11	#[derive(Debug)]
12	pub enum ConfigError {
13		/// Threads count must be greater than zero.
14		InvalidThreadCount {
15			description("Field thread_count shall be positive")
16		}
17	}
18}
19
20
21impl Config {
22	/// Creates new configuration structure. 
23	/// First parameter - number of threads. You can get this value from `num_cpus` crate.
24	pub fn new(thread_count: usize) -> Result<Config,ConfigError> {
25		let result = Config {
26			thread_count: thread_count,
27			limit_result_channel_buffer: 0,
28			limit_task_channel_buffer: 0,
29		};
30		result.check_configuration()?;
31		Ok(result)
32	}
33
34	fn check_configuration(&self) -> Result<(),ConfigError> {
35		if self.thread_count <= 0 {
36			return Err(ConfigError::InvalidThreadCount);
37		}
38		Ok(())
39	}
40
41	/// Returns number of used threads.
42	pub fn get_thread_count(&self) -> usize {
43		return self.thread_count;
44	}
45	
46	/// Set limit for sync channel of result values.
47	pub fn set_limit_result_channel(&mut self,value: usize) -> Result<&mut Self,ConfigError> {
48		self.limit_result_channel_buffer = value;
49		self.check_configuration()?;
50		Ok(self)
51	}
52
53	/// Returns limit for sync channel of result values.
54	pub fn get_limit_result_channel(&self) -> usize {
55		return self.limit_result_channel_buffer;
56	}
57
58	/// Set limit for sync channel of tasks.
59	pub fn set_limit_task_channel(&mut self,value: usize) -> Result<&mut Self,ConfigError> {
60		self.limit_task_channel_buffer = value;
61		self.check_configuration()?;
62		Ok(self)
63	}
64
65	/// Returns limit for sync channel of tasks.
66	pub fn get_limit_task_channel(&self) -> usize {
67		return self.limit_task_channel_buffer;
68	}
69}