Diff
Not logged in

Differences From Artifact [441a41c46e]:

To Artifact [9ed85d68a9]:


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
..
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
..
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149

150



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169

170
171
172
173
174
175

176
177
178
179
180
181
182
183
184
185
// Copyright 2016 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under (1) the MaidSafe.net
// Commercial License, version 1.0 or later, or (2) The General Public License
// (GPL), version 3, depending on which licence you accepted on initial access
// to the Software (the "Licences").
//
// By contributing code to the SAFE Network Software, or to this project
// generally, you agree to be bound by the terms of the MaidSafe Contributor
// Agreement, version 1.0.
// This, along with the Licenses can be found in the root directory of this
// project at LICENSE, COPYING and CONTRIBUTOR.
//
// Unless required by applicable law or agreed to in writing, the SAFE Network
// Software distributed under the GPL Licence is distributed on an "AS IS"
// BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied.
//
// Please review the Licences for the specific language governing permissions
// and limitations relating to use of the SAFE Network Software.

//! Test utilities.


use repr_c::ReprC;
use std::fmt::Debug;
use std::os::raw::c_void;
use std::slice;
use std::sync::mpsc::{self, Sender};

/// Convert a `mpsc::Sender<T>` to a void ptr which can be passed as user data to
................................................................................
    unwrap!((*tx).send(value));
}

/// Call a FFI function and block until its callback gets called.
/// Use this if the callback accepts no arguments in addition to `user_data`
/// and `error_code`.
pub fn call_0<F>(f: F) -> Result<(), i32>
    where F: FnOnce(*mut c_void, extern "C" fn(*mut c_void, i32))
{
    let (tx, rx) = mpsc::channel::<i32>();
    f(sender_as_user_data(&tx), callback_0);

    let error = unwrap!(rx.recv());
    if error == 0 { Ok(()) } else { Err(error) }
}

/// Call an FFI function and block until its callback gets called, then return
/// the argument which were passed to that callback.
/// Use this if the callback accepts one argument in addition to `user_data`
/// and `error_code`.
pub unsafe fn call_1<F, E: Debug, T>(f: F) -> Result<T, i32>
    where F: FnOnce(*mut c_void, extern "C" fn(*mut c_void, i32, T::C)),
          T: ReprC<Error = E>
{
    let (tx, rx) = mpsc::channel::<SendWrapper<Result<T, i32>>>();
    f(sender_as_user_data(&tx), callback_1::<E, T>);
    unwrap!(rx.recv()).0
}

/// Call a FFI function and block until its callback gets called, then return
/// the argument which were passed to that callback.
/// Use this if the callback accepts two arguments in addition to `user_data`
/// and `error_code`.
pub unsafe fn call_2<F, E0, E1, T0, T1>(f: F) -> Result<(T0, T1), i32>
    where F: FnOnce(*mut c_void, extern "C" fn(*mut c_void, i32, T0::C, T1::C)),

          E0: Debug,
          E1: Debug,
          T0: ReprC<Error = E0>,
          T1: ReprC<Error = E1>
{
    let (tx, rx) = mpsc::channel::<SendWrapper<Result<(T0, T1), i32>>>();
    f(sender_as_user_data(&tx), callback_2::<E0, E1, T0, T1>);
................................................................................
}

/// Call a FFI function and block until its callback gets called, then copy
/// the array argument which was passed to `Vec<T>` and return the result.
/// Use this if the callback accepts `*const T` and `usize` (length) arguments in addition
/// to `user_data` and `error_code`.
pub unsafe fn call_vec<F, E, T, U>(f: F) -> Result<Vec<T>, i32>
    where F: FnOnce(*mut c_void, extern "C" fn(*mut c_void, i32, T::C, usize)),

          E: Debug,
          T: ReprC<C = *const U, Error = E>
{
    let (tx, rx) = mpsc::channel::<SendWrapper<Result<Vec<T>, i32>>>();

    f(sender_as_user_data(&tx), callback_vec::<E, T, U>);
    unwrap!(rx.recv()).0
}

/// Call a FFI function and block until its callback gets called, then copy
/// the byte array argument which was passed to `Vec<u8>` and return the result.
pub unsafe fn call_vec_u8<F>(f: F) -> Result<Vec<u8>, i32>
    where F: FnOnce(*mut c_void,
                    extern "C" fn(*mut c_void, i32, *const u8, usize))
{
    let (tx, rx) = mpsc::channel::<Result<Vec<u8>, i32>>();
    f(sender_as_user_data(&tx), callback_vec_u8);
    unwrap!(rx.recv())
}


extern "C" fn callback_0(user_data: *mut c_void, error: i32) {
    unsafe { send_via_user_data(user_data, error) }
}


extern "C" fn callback_1<E, T>(user_data: *mut c_void, error: i32, arg: T::C)
    where E: Debug,
          T: ReprC<Error = E>
{
    unsafe {
        let result: Result<T, i32> = if error == 0 {
            Ok(unwrap!(T::clone_from_repr_c(arg)))
        } else {
            Err(error)
        };
        send_via_user_data(user_data, SendWrapper(result));
    }
}


extern "C" fn callback_2<E0, E1, T0, T1>(user_data: *mut c_void,
                                         error: i32,

                                         arg0: T0::C,
                                         arg1: T1::C)
    where E0: Debug,
          E1: Debug,
          T0: ReprC<Error = E0>,
          T1: ReprC<Error = E1>
{
    unsafe {
        let result: Result<(T0, T1), i32> = if error == 0 {
            Ok((unwrap!(T0::clone_from_repr_c(arg0)), unwrap!(T1::clone_from_repr_c(arg1))))
        } else {
            Err(error)
        };
        send_via_user_data(user_data, SendWrapper(result))
    }
}


extern "C" fn callback_vec<E, T, U>(user_data: *mut c_void, error: i32, array: T::C, size: usize)



    where E: Debug,
          T: ReprC<C = *const U, Error = E>
{
    unsafe {
        let result: Result<Vec<T>, i32> = if error == 0 {
            let slice_ffi = slice::from_raw_parts(array, size);
            let mut vec = Vec::with_capacity(slice_ffi.len());
            for elt in slice_ffi {
                vec.push(unwrap!(T::clone_from_repr_c(elt)));
            }
            Ok(vec)
        } else {
            Err(error)
        };

        send_via_user_data(user_data, SendWrapper(result))
    }
}


extern "C" fn callback_vec_u8(user_data: *mut c_void, error: i32, ptr: *const u8, len: usize) {
    unsafe {
        let result = if error == 0 {
            Ok(slice::from_raw_parts(ptr, len).to_vec())
        } else {
            Err(error)

        };

        send_via_user_data(user_data, result)
    }
}

// Unsafe wrapper for passing non-Send types through mpsc channels.
// Use with caution!
struct SendWrapper<T>(T);
unsafe impl<T> Send for SendWrapper<T> {}


|
|
<
|

|
|
<
<
|

|
|
|
<

|
|



>







 







|













|












|
>







 







|
>













|






>
|
|


>
|




|


|





>

<
>








|


|





>
|
>
>
>




|







|






>
|

|


<
>










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
..
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
..
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181

182
183
184
185
186
187
188
189
190
191
192
// Copyright 2016 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under (1) the MaidSafe.net Commercial License,
// version 1.0 or later, or (2) The General Public License (GPL), version 3, depending on which

// licence you accepted on initial access to the Software (the "Licences").
//
// By contributing code to the SAFE Network Software, or to this project generally, you agree to be
// bound by the terms of the MaidSafe Contributor Agreement.  This, along with the Licenses can be


// found in the root directory of this project at LICENSE, COPYING and CONTRIBUTOR.
//
// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.

//
// Please review the Licences for the specific language governing permissions and limitations
// relating to use of the SAFE Network Software.

//! Test utilities.

use super::FfiResult;
use repr_c::ReprC;
use std::fmt::Debug;
use std::os::raw::c_void;
use std::slice;
use std::sync::mpsc::{self, Sender};

/// Convert a `mpsc::Sender<T>` to a void ptr which can be passed as user data to
................................................................................
    unwrap!((*tx).send(value));
}

/// Call a FFI function and block until its callback gets called.
/// Use this if the callback accepts no arguments in addition to `user_data`
/// and `error_code`.
pub fn call_0<F>(f: F) -> Result<(), i32>
    where F: FnOnce(*mut c_void, extern "C" fn(*mut c_void, FfiResult))
{
    let (tx, rx) = mpsc::channel::<i32>();
    f(sender_as_user_data(&tx), callback_0);

    let error = unwrap!(rx.recv());
    if error == 0 { Ok(()) } else { Err(error) }
}

/// Call an FFI function and block until its callback gets called, then return
/// the argument which were passed to that callback.
/// Use this if the callback accepts one argument in addition to `user_data`
/// and `error_code`.
pub unsafe fn call_1<F, E: Debug, T>(f: F) -> Result<T, i32>
    where F: FnOnce(*mut c_void, extern "C" fn(*mut c_void, FfiResult, T::C)),
          T: ReprC<Error = E>
{
    let (tx, rx) = mpsc::channel::<SendWrapper<Result<T, i32>>>();
    f(sender_as_user_data(&tx), callback_1::<E, T>);
    unwrap!(rx.recv()).0
}

/// Call a FFI function and block until its callback gets called, then return
/// the argument which were passed to that callback.
/// Use this if the callback accepts two arguments in addition to `user_data`
/// and `error_code`.
pub unsafe fn call_2<F, E0, E1, T0, T1>(f: F) -> Result<(T0, T1), i32>
    where F: FnOnce(*mut c_void,
                    extern "C" fn(*mut c_void, FfiResult, T0::C, T1::C)),
          E0: Debug,
          E1: Debug,
          T0: ReprC<Error = E0>,
          T1: ReprC<Error = E1>
{
    let (tx, rx) = mpsc::channel::<SendWrapper<Result<(T0, T1), i32>>>();
    f(sender_as_user_data(&tx), callback_2::<E0, E1, T0, T1>);
................................................................................
}

/// Call a FFI function and block until its callback gets called, then copy
/// the array argument which was passed to `Vec<T>` and return the result.
/// Use this if the callback accepts `*const T` and `usize` (length) arguments in addition
/// to `user_data` and `error_code`.
pub unsafe fn call_vec<F, E, T, U>(f: F) -> Result<Vec<T>, i32>
    where F: FnOnce(*mut c_void,
                    extern "C" fn(*mut c_void, FfiResult, T::C, usize)),
          E: Debug,
          T: ReprC<C = *const U, Error = E>
{
    let (tx, rx) = mpsc::channel::<SendWrapper<Result<Vec<T>, i32>>>();

    f(sender_as_user_data(&tx), callback_vec::<E, T, U>);
    unwrap!(rx.recv()).0
}

/// Call a FFI function and block until its callback gets called, then copy
/// the byte array argument which was passed to `Vec<u8>` and return the result.
pub unsafe fn call_vec_u8<F>(f: F) -> Result<Vec<u8>, i32>
    where F: FnOnce(*mut c_void,
                    extern "C" fn(*mut c_void, FfiResult, *const u8, usize))
{
    let (tx, rx) = mpsc::channel::<Result<Vec<u8>, i32>>();
    f(sender_as_user_data(&tx), callback_vec_u8);
    unwrap!(rx.recv())
}

#[cfg_attr(feature="cargo-clippy", allow(needless_pass_by_value))]
extern "C" fn callback_0(user_data: *mut c_void, res: FfiResult) {
    unsafe { send_via_user_data(user_data, res.error_code) }
}

#[cfg_attr(feature="cargo-clippy", allow(needless_pass_by_value))]
extern "C" fn callback_1<E, T>(user_data: *mut c_void, res: FfiResult, arg: T::C)
    where E: Debug,
          T: ReprC<Error = E>
{
    unsafe {
        let result: Result<T, i32> = if res.error_code == 0 {
            Ok(unwrap!(T::clone_from_repr_c(arg)))
        } else {
            Err(res.error_code)
        };
        send_via_user_data(user_data, SendWrapper(result));
    }
}

#[cfg_attr(feature="cargo-clippy", allow(needless_pass_by_value))]
extern "C" fn callback_2<E0, E1, T0, T1>(user_data: *mut c_void,

                                         res: FfiResult,
                                         arg0: T0::C,
                                         arg1: T1::C)
    where E0: Debug,
          E1: Debug,
          T0: ReprC<Error = E0>,
          T1: ReprC<Error = E1>
{
    unsafe {
        let result: Result<(T0, T1), i32> = if res.error_code == 0 {
            Ok((unwrap!(T0::clone_from_repr_c(arg0)), unwrap!(T1::clone_from_repr_c(arg1))))
        } else {
            Err(res.error_code)
        };
        send_via_user_data(user_data, SendWrapper(result))
    }
}

#[cfg_attr(feature="cargo-clippy", allow(needless_pass_by_value))]
extern "C" fn callback_vec<E, T, U>(user_data: *mut c_void,
                                    res: FfiResult,
                                    array: T::C,
                                    size: usize)
    where E: Debug,
          T: ReprC<C = *const U, Error = E>
{
    unsafe {
        let result: Result<Vec<T>, i32> = if res.error_code == 0 {
            let slice_ffi = slice::from_raw_parts(array, size);
            let mut vec = Vec::with_capacity(slice_ffi.len());
            for elt in slice_ffi {
                vec.push(unwrap!(T::clone_from_repr_c(elt)));
            }
            Ok(vec)
        } else {
            Err(res.error_code)
        };

        send_via_user_data(user_data, SendWrapper(result))
    }
}

#[cfg_attr(feature = "cargo-clippy", allow(needless_pass_by_value))]
extern "C" fn callback_vec_u8(user_data: *mut c_void, res: FfiResult, ptr: *const u8, len: usize) {
    unsafe {
        let result = if res.error_code == 0 {
            Ok(slice::from_raw_parts(ptr, len).to_vec())
        } else {

            Err(res.error_code)
        };

        send_via_user_data(user_data, result)
    }
}

// Unsafe wrapper for passing non-Send types through mpsc channels.
// Use with caution!
struct SendWrapper<T>(T);
unsafe impl<T> Send for SendWrapper<T> {}