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
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135

//! Error structures used in cluFlock methods.
//!

use crate::SafeUnlockFlock;
use crate::sys::FlockElement;
use std::ops::Deref;
use std::io;


/// The standard error for Flock methods, from the error you can get a borrowed value.
#[derive(Debug)]
pub struct FlockError<T> where T: FlockElement {
	data: T,
	err: io::Error,
}

impl<T> FlockError<T> where T: FlockElement {
	#[inline]
	pub fn new(a: T, err: io::Error) -> Self {
		Self {
			data: a,
			err: err,
		}
	}
	
	/// Retrieve only the data from the error structure.
	#[inline(always)]
	pub fn into(self) -> T {
		self.data
	}
	
	/// Get only the error from the error structure.
	#[inline(always)]
	pub fn err(self) -> io::Error {
		self.err
	}
	
	/// Get all data from the error structure.
	#[inline(always)]
	pub fn all(self) -> (T, io::Error) {
		(self.data, self.err)
	}
}

impl<T> From<FlockError<T>> for io::Error where T: FlockElement {
	#[inline(always)]
	fn from(a: FlockError<T>) -> io::Error {
		a.err
	}
}


impl<T> Deref for FlockError<T> where T: FlockElement {
	type Target = io::Error;
	
	#[inline(always)]
	fn deref(&self) -> &Self::Target {
		&self.err
	}
}

///The standard error for FlockFn! methods, from the error you can get a borrowed value.
#[derive(Debug)]
pub struct FlockFnError<D, F, Fr> where D: FlockElement, F: FnOnce(SafeUnlockFlock<D>) -> Fr {
	data: FlockError<D>,
	function: F,
}

impl<D, F, Fr> FlockFnError<D, F, Fr> where D: FlockElement, F: FnOnce(SafeUnlockFlock<D>) -> Fr {
	#[inline(always)]
	pub fn new(data: D, function: F, err: io::Error) -> Self {
		Self::flock_error(FlockError::new(data, err), function)
	}
	
	#[inline]
	pub fn flock_error(data: FlockError<D>, function: F) -> Self {
		Self {
			data: data,
			function: function,
		}	
	}
	
	#[inline(always)]
	pub fn into_flock_error(self) -> FlockError<D> {
		self.data
	}
	
	/// Retrieve only the data from the error structure.
	#[inline(always)]
	pub fn into(self) -> D {
		self.data.into()
	}
	
	/// Get only not executed FnOnce from the error structure.
	#[inline(always)]
	pub fn function(self) -> F {
		self.function
	}
	
	/// Get only the error from the error structure.
	#[inline(always)]
	pub fn err(self) -> io::Error {
		self.data.err()
	}
	
	/// Get all the data from the error.
	#[inline(always)]
	pub fn all(self) -> (D, F, io::Error) {
		(self.data.data, self.function, self.data.err)
	}
	
	/// Get all the data from the error structure as it is.
	#[inline(always)]
	pub fn raw_all(self) -> (FlockError<D>, F) {
		(self.data, self.function)
	}
}


impl<D, F, Fr> Deref for FlockFnError<D, F, Fr> where D: FlockElement, F: FnOnce(SafeUnlockFlock<D>) -> Fr {
	type Target = io::Error;
	
	#[inline(always)]
	fn deref(&self) -> &Self::Target {
		&self.data.err
	}
}

impl<D, F, Fr> From<FlockFnError<D, F, Fr>> for io::Error where D: FlockElement, F: FnOnce(SafeUnlockFlock<D>) -> Fr {
	#[inline(always)]
	fn from(a: FlockFnError<D, F, Fr>) -> io::Error {
		a.data.err
	}
}