1#[macro_export]
16macro_rules! error_trace_function {
17 () => {{
18 fn f() {}
19 fn type_name_of<T>(_: T) -> &'static str {
20 std::any::type_name::<T>()
21 }
22 let name = type_name_of(f);
23 name.strip_suffix("::f").unwrap()
24 }};
25}
26
27#[macro_export]
29macro_rules! error_trace_new {
30 ( $message:expr ) => {
31 $crate::ErrorTrace::new(format!("{}: {}", $crate::error_trace_function!(), $message,))
32 };
33}
34
35#[macro_export]
37macro_rules! error_trace_new_with_error {
38 ( $message:expr, $error:expr ) => {
39 $crate::ErrorTrace::new(format!(
40 "{}: {} with error: {}",
41 $crate::error_trace_function!(),
42 $message,
43 $error.to_string(),
44 ))
45 };
46}
47
48#[macro_export]
50macro_rules! error_trace_add_frame {
51 ( $error:expr, $message:expr ) => {
52 $error.add_frame(format!("{}: {}", $crate::error_trace_function!(), $message,))
53 };
54}
55
56#[macro_export]
58macro_rules! data_stream_get_size {
59 ( $data_stream:expr ) => {
60 match $data_stream.write() {
61 Ok(mut data_stream) => match data_stream.get_size() {
62 Ok(size) => size,
63 Err(mut error) => {
64 $crate::error_trace_add_frame!(
65 error,
66 "Unable to determine size of data stream"
67 );
68 return Err(error);
69 }
70 },
71 Err(error) => {
72 return Err($crate::error_trace_new_with_error!(
73 "Unable to obtain write lock on data stream",
74 error
75 ));
76 }
77 };
78 };
79}
80
81#[macro_export]
83macro_rules! data_stream_read_at_position {
84 ( $data_stream:expr, $buf:expr, $pos:expr ) => {
85 match $data_stream.write() {
86 Ok(mut data_stream) => match data_stream.read_at_position($buf, $pos) {
87 Ok(read_count) => read_count,
88 Err(mut error) => {
89 $crate::error_trace_add_frame!(error, "Unable to read from data stream");
90 return Err(error);
91 }
92 },
93 Err(error) => {
94 return Err($crate::error_trace_new_with_error!(
95 "Unable to obtain write lock on data stream",
96 error
97 ));
98 }
99 };
100 };
101}
102
103#[macro_export]
105macro_rules! data_stream_read_exact_at_position {
106 ( $data_stream:expr, $buf:expr, $pos:expr ) => {
107 match $data_stream.write() {
108 Ok(mut data_stream) => match data_stream.read_exact_at_position($buf, $pos) {
109 Ok(offset) => offset,
110 Err(mut error) => {
111 $crate::error_trace_add_frame!(error, "Unable to read from data stream");
112 return Err(error);
113 }
114 },
115 Err(error) => {
116 return Err($crate::error_trace_new_with_error!(
117 "Unable to obtain write lock on data stream",
118 error
119 ));
120 }
121 };
122 };
123}