1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 /// This macro is very similar to xpcom_macro, but works a bit differently:
7 /// When possible, it returns errors via the callback rather than via the return
10 /// It implicitly adds the callback argument of type: nsIBitsNewRequestCallback
12 /// It needs an action type, to be specified before the rust name, in square
15 /// The rustic implementation that the xpcom method calls is expected to return
16 /// the type: Result<_, BitsTaskError>. If this value is Ok, it will be ignored.
17 /// If the value is Err, it will be returned via the callback passed.
24 /// rust_method => XpcomMethod(
25 /// foo: *const nsACString,
26 /// bar: *const nsIBar,
28 /// [optional] qux: *const nsIQux,
33 /// Results in the macro generating a method like:
36 /// unsafe fn XpcomMethod(
38 /// foo: *const nsACString,
39 /// bar: *const nsIBar,
41 /// qux: *const nsIQux,
42 /// callback: *const nsIBitsNewRequestCallback,
44 /// let callback: &nsIBitsNewRequestCallback = match xpcom::Ensure::ensure(callback) {
46 /// Err(result) => return result,
48 /// let foo = match xpcom::Ensure::ensure(foo) {
51 /// dispatch_pretask_interface_error(BitsTaskError::new(ErrorType::NullArgument, ActionType.into(), ErrorStage::Pretask), callback);
55 /// let bar = match xpcom::Ensure::ensure(bar) {
58 /// dispatch_pretask_interface_error(BitsTaskError::new(ErrorType::NullArgument, ActionType.into(), ErrorStage::Pretask), callback);
62 /// let baz = match xpcom::Ensure::ensure(baz) {
65 /// dispatch_pretask_interface_error(BitsTaskError::new(ErrorType::NullArgument, ActionType.into(), ErrorStage::Pretask), callback);
69 /// let qux = match xpcom::Ensure::ensure(qux) {
70 /// Ok(val) => Some(val),
74 /// if let Err(error) = self.rust_method(foo, bar, baz, qux, callback) {
75 /// dispatch_pretask_interface_error(error, callback);
82 /// Which expects a Rustic implementation method like:
90 /// qux: Option<&nsIQux>,
91 /// callback: &nsIBitsNewRequestCallback,
92 /// ) -> Result<(), BitsTaskError> {
97 macro_rules! nsIBits_method {
98 // The internal rule @ensure_param converts raw pointer arguments to
99 // references, calling dispatch_pretask_interface_error and returning if the
101 // If, however, the type is optional, the reference will also be wrapped
102 // in an option and null pointers will be converted to None.
103 (@ensure_param [optional] $name:ident, $action:expr, $callback:ident) => {
104 let $name = match Ensure::ensure($name) {
105 Ok(val) => Some(val),
109 (@ensure_param $name:ident, $action:expr, $callback:ident) => {
110 let $name = match Ensure::ensure($name) {
113 dispatch_pretask_interface_error(BitsTaskError::new(NullArgument, $action.into(), Pretask), $callback);
119 ([$action:expr] $rust_name:ident => $xpcom_name:ident($($([$param_required:ident])* $param_name:ident: $param_type:ty $(,)*)*)) => {
120 #[allow(non_snake_case)]
121 unsafe fn $xpcom_name(&self, $($param_name: $param_type, )* callback: *const nsIBitsNewRequestCallback) -> nsresult {
124 // When no params are passed, the imports below will not be used, so silence the
126 #[allow(unused_imports)]
127 use bits_interface::{
128 dispatch_callback::dispatch_pretask_interface_error,
129 error::{BitsTaskError, ErrorStage::Pretask, ErrorType::NullArgument},
132 let callback: &nsIBitsNewRequestCallback = match Ensure::ensure(callback) {
134 Err(result) => return result,
136 $(nsIBits_method!(@ensure_param $([$param_required])* $param_name, $action, callback);)*
137 if let Err(error) = self.$rust_name($($param_name, )* callback) {
138 dispatch_pretask_interface_error(error, callback);
146 * Same as above, but expects a nsIBitsCallback as its callback.
149 macro_rules! nsIBitsRequest_method {
150 // The internal rule @ensure_param converts raw pointer arguments to
151 // references, calling dispatch_pretask_interface_error and returning if the
153 // If, however, the type is optional, the reference will also be wrapped
154 // in an option and null pointers will be converted to None.
155 (@ensure_param [optional] $name:ident, $action:expr, $callback:ident) => {
156 let $name = match Ensure::ensure($name) {
157 Ok(val) => Some(val),
161 (@ensure_param $name:ident, $action:expr, $callback:ident) => {
162 let $name = match Ensure::ensure($name) {
165 dispatch_pretask_request_error(BitsTaskError::new(NullArgument, $action.into(), Pretask), $callback);
171 ([$action:expr] $rust_name:ident => $xpcom_name:ident($($([$param_required:ident])* $param_name:ident: $param_type:ty $(,)*)*)) => {
172 #[allow(non_snake_case)]
173 unsafe fn $xpcom_name(&self, $($param_name: $param_type, )* callback: *const nsIBitsCallback) -> nsresult {
176 // When no params are passed, the imports below will not be used, so silence the
178 #[allow(unused_imports)]
179 use bits_interface::{
180 dispatch_callback::dispatch_pretask_request_error,
181 error::{BitsTaskError, ErrorStage::Pretask, ErrorType::NullArgument},
184 let callback: &nsIBitsCallback = match Ensure::ensure(callback) {
186 Err(result) => return result,
188 $(nsIBitsRequest_method!(@ensure_param $([$param_required])* $param_name, $action, callback);)*
189 if let Err(error) = self.$rust_name($($param_name, )* callback) {
190 dispatch_pretask_request_error(error, callback);