sync-ptr 0.2.1

Sync & Send wrappers for raw pointer's and function pointers in rust
Documentation
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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
extern crate alloc;
extern crate std;

use alloc::{format, vec};
use core::ffi::c_void;
use core::ptr::null_mut;
use std::mem;
use std::mem::{align_of, size_of};
use sync_ptr::*;

#[test]
pub fn test_debug() {
    let n = null_mut::<c_void>().as_sync_mut();
    assert_eq!(format!("{:?}", n), "SyncMutPtr(0x0)");
    let n = null_mut::<c_void>().as_sync_const();
    assert_eq!(format!("{:?}", n), "SyncConstPtr(0x0)");
    let n = null_mut::<c_void>().as_send_const();
    assert_eq!(format!("{:?}", n), "SendConstPtr(0x0)");
    let n = null_mut::<c_void>().as_send_mut();
    assert_eq!(format!("{:?}", n), "SendMutPtr(0x0)");

    #[cfg(target_pointer_width = "64")]
    {
        let n = null_mut::<c_void>().as_sync_mut();
        assert_eq!(
            format!("{:#?}", n),
            "SyncMutPtr(\n    0x0000000000000000,\n)"
        );
        let n = null_mut::<c_void>().as_sync_const();
        assert_eq!(
            format!("{:#?}", n),
            "SyncConstPtr(\n    0x0000000000000000,\n)"
        );
        let n = null_mut::<c_void>().as_send_const();
        assert_eq!(
            format!("{:#?}", n),
            "SendConstPtr(\n    0x0000000000000000,\n)"
        );
        let n = null_mut::<c_void>().as_send_mut();
        assert_eq!(
            format!("{:#?}", n),
            "SendMutPtr(\n    0x0000000000000000,\n)"
        );
    }

    #[cfg(target_pointer_width = "32")]
    {
        let n = null_mut::<c_void>().as_sync_mut();
        assert_eq!(format!("{:#?}", n), "SyncMutPtr(\n    0x00000000,\n)");
        let n = null_mut::<c_void>().as_sync_const();
        assert_eq!(format!("{:#?}", n), "SyncConstPtr(\n    0x00000000,\n)");
        let n = null_mut::<c_void>().as_send_const();
        assert_eq!(format!("{:#?}", n), "SendConstPtr(\n    0x00000000,\n)");
        let n = null_mut::<c_void>().as_send_mut();
        assert_eq!(format!("{:#?}", n), "SendMutPtr(\n    0x00000000,\n)");
    }
}

#[test]
pub fn test_cmp() {
    unsafe {
        let mut data = vec![0; 4096];
        let n = data.as_mut_ptr().as_sync_mut();
        let x = n.add(5).as_sync_mut();
        assert!(x > n);
        assert!(n < x);
        assert_ne!(n, x);
        assert_eq!(n, n);
        assert_eq!(x, x);
    }

    unsafe {
        let mut data = vec![0; 4096];
        let n = data.as_mut_ptr().as_send_mut();
        let x = n.add(5).as_send_mut();
        assert!(x > n);
        assert!(n < x);
        assert_ne!(n, x);
        assert_eq!(n, n);
        assert_eq!(x, x);
    }

    unsafe {
        let mut data = vec![0; 4096];
        let n = data.as_mut_ptr().as_send_const();
        let x = n.add(5).as_send_const();
        assert!(x > n);
        assert!(n < x);
        assert_ne!(n, x);
        assert_eq!(n, n);
        assert_eq!(x, x);
    }

    unsafe {
        let mut data = vec![0; 4096];
        let n = data.as_mut_ptr().as_sync_const();
        let x = n.add(5).as_sync_const();
        assert!(x > n);
        assert!(n < x);
        assert_ne!(n, x);
        assert_eq!(n, n);
        assert_eq!(x, x);
    }
}

#[test]
fn test() {
    unsafe {
        assert_eq!(size_of::<SyncConstPtr<c_void>>(), size_of::<*mut c_void>());
        assert_eq!(
            align_of::<SyncConstPtr<c_void>>(),
            align_of::<*mut c_void>()
        );

        let mut value = 45;
        let n: *mut u64 = &mut value;
        let x = n.as_sync_const();
        assert_eq!(x, x);
        let y = n.as_send_mut();
        let z = y.as_sync_const();
        let u = z.as_send_mut();
        assert_eq!(x, z);
        assert_eq!(u, u);

        assert_eq!(z.read(), 45);
        assert_eq!(z.inner().read(), 45);
        std::thread::spawn(move || assert!(!u.inner().is_null()))
            .join()
            .unwrap();
    }
}

struct RustControlStructureThatIsNowSend {
    some_handle: SendConstPtr<c_void>,
    some_rust_data: u64,
}

#[test]
fn example() {
    //Some handle or pointer obtained goes here.
    let handle: *mut c_void = null_mut();
    let data: u64 = 123u64;

    let rcs = RustControlStructureThatIsNowSend {
        some_handle: handle.as_send_const(),
        some_rust_data: data,
    };

    //Every *const T and *mut T has these fn's now.
    let _sync_const: SyncConstPtr<c_void> = handle.as_sync_const();
    let _send_const: SendConstPtr<c_void> = handle.as_send_const();

    //Every *mut T has these fn's now.
    let _sync_mut: SyncMutPtr<c_void> = handle.as_sync_mut();
    let _send_mut: SendMutPtr<c_void> = handle.as_send_mut();

    //The other Ptr types have the same constructors too.
    let _send_const_null: SendMutPtr<c_void> = SendMutPtr::null();
    let _send_const_new: SendMutPtr<c_void> = SendMutPtr::new(null_mut());
    let _from_address: SyncMutPtr<c_void> = SyncMutPtr::from_address(0usize);

    std::thread::spawn(move || {
        assert!(rcs.some_handle.is_null()); //Use boxed
        let _unwrapped: *const c_void = rcs.some_handle.inner(); //unwrap if you want
        let _unwrapped2: *const c_void = rcs.some_handle.into(); //Into<*const T> is also implemented. (*mut T too when applicable)
        let _address: usize = rcs.some_handle.as_address(); //Into<usize> is also implemented.
        let casted: SendConstPtr<usize> = rcs.some_handle.cast::<usize>(); //Cast if you want.
        unsafe {
            if !casted.is_null() {
                //In this example this is obviously always null...
                casted.read_volatile(); //Read if you want
            }
        }

        assert_eq!(rcs.some_rust_data, 123u64)
    })
    .join()
    .unwrap();
}

#[test]
fn test_addr() {
    let test_addr: *const usize = 5158485 as _;
    let sync = test_addr.as_sync_const();
    assert_eq!(sync.as_address(), test_addr as usize);
}

#[inline(always)]
extern "C" fn test_function() -> u64 {
    123456u64
}

#[inline(always)]
#[cfg(feature = "fnptr")]
extern "C" fn test_function2() -> u64 {
    1234567u64
}
#[test]
fn test_function_pointer() {
    let test_fn_ptr: SyncMutPtr<c_void> = SyncMutPtr::new(test_function as *mut c_void);
    let result =
        unsafe { mem::transmute::<*mut c_void, extern "C" fn() -> u64>(test_fn_ptr.inner()) }();

    assert_eq!(result, test_function());
}

#[test]
#[cfg(feature = "fnptr")]
fn test_function_pointer2() {
    let test_fn_ptr = sync_fn_ptr!(extern "C" fn() -> u64, test_function);
    assert!(!test_fn_ptr.is_null());
    assert_eq!(test_fn_ptr(), test_function());
}

#[test]
#[cfg(feature = "fnptr")]
fn test_function_pointer3() {
    let n = test_function as *const c_void;

    let test_fn_ptr = unsafe { sync_fn_ptr_from_addr!(extern "C" fn() -> u64, n) };

    assert!(!test_fn_ptr.is_null());
    assert_eq!(test_fn_ptr(), test_function());
}

#[test]
#[cfg(feature = "fnptr")]
fn test_function_pointer_null() {
    let test_fn_ptr = unsafe { sync_fn_ptr_from_addr!(extern "C" fn() -> u64, 0) };

    assert!(test_fn_ptr.is_null());

    let r = std::panic::catch_unwind(move || {
        test_fn_ptr();
    });

    assert!(r.is_err());

    let test_fn_ptr2 = SyncFnPtr::<extern "C" fn() -> u64>::default();

    let r = std::panic::catch_unwind(move || {
        test_fn_ptr2();
    });

    assert!(r.is_err());

    assert_eq!(test_fn_ptr, test_fn_ptr2);
}

#[test]
#[cfg(feature = "fnptr")]
fn test_function_pointer_equal() {
    let n = test_function as *const c_void;
    let test_fn_ptr = unsafe { sync_fn_ptr_from_addr!(extern "C" fn() -> u64, n) };
    let test_fn_ptr2 = sync_fn_ptr!(extern "C" fn() -> u64, test_function);

    //Compiler doesn't guarantee this...
    //assert_eq!(test_fn_ptr, test_fn_ptr2);
    //assert_eq!(test_fn_ptr.as_address(), test_fn_ptr2.as_address());

    if test_fn_ptr == test_fn_ptr2
        || test_fn_ptr.as_address() == test_fn_ptr2.as_address()
        || n as usize == test_function as usize
    {
        //But this is guaranteed
        assert_eq!(test_fn_ptr, test_fn_ptr2);
        assert_eq!(test_fn_ptr.as_address(), test_fn_ptr2.as_address());
    }

    let test_fn_ptr3 = sync_fn_ptr!(extern "C" fn() -> u64, test_function2);
    assert_ne!(test_fn_ptr, test_fn_ptr3);
    assert_ne!(test_fn_ptr.as_address(), test_fn_ptr3.as_address());
}

#[test]
fn snd_test_function_pointer() {
    let test_fn_ptr: SendMutPtr<c_void> = SendMutPtr::new(test_function as *mut c_void);
    let result =
        unsafe { mem::transmute::<*mut c_void, extern "C" fn() -> u64>(test_fn_ptr.inner()) }();

    assert_eq!(result, test_function());
}

#[test]
#[cfg(feature = "fnptr")]
fn snd_test_function_pointer2() {
    let test_fn_ptr = send_fn_ptr!(extern "C" fn() -> u64, test_function);
    assert!(!test_fn_ptr.is_null());
    assert_eq!(test_fn_ptr(), test_function());
}

#[test]
#[cfg(feature = "fnptr")]
fn snd_test_function_pointer3() {
    let n = test_function as *const c_void;

    let test_fn_ptr = unsafe { send_fn_ptr_from_addr!(extern "C" fn() -> u64, n) };

    assert!(!test_fn_ptr.is_null());
    assert_eq!(test_fn_ptr(), test_function());
}

#[test]
#[cfg(feature = "fnptr")]
fn snd_test_function_pointer_null() {
    let test_fn_ptr = unsafe { send_fn_ptr_from_addr!(extern "C" fn() -> u64, 0) };

    assert!(test_fn_ptr.is_null());
    assert!(test_fn_ptr.is_null());
    assert_eq!(test_fn_ptr.as_address(), 0);
    assert_eq!(test_fn_ptr.as_raw_ptr(), std::ptr::null());
    assert!(test_fn_ptr.inner().is_none());

    let r = std::panic::catch_unwind(move || {
        test_fn_ptr();
    });

    assert!(r.is_err());

    let test_fn_ptr2 = SendFnPtr::<extern "C" fn() -> u64>::default();

    assert!(test_fn_ptr2.is_null());
    assert_eq!(test_fn_ptr2.as_address(), 0);
    assert_eq!(test_fn_ptr2.as_raw_ptr(), std::ptr::null());
    assert!(test_fn_ptr2.inner().is_none());
    let r = std::panic::catch_unwind(move || {
        test_fn_ptr2();
    });

    assert!(r.is_err());

    assert_eq!(test_fn_ptr, test_fn_ptr2);

    let test_fn_ptr3 = SendFnPtr::<extern "C" fn() -> u64>::null();
    assert!(test_fn_ptr3.is_null());
    assert_eq!(test_fn_ptr3.as_address(), 0);
    assert_eq!(test_fn_ptr3.as_raw_ptr(), std::ptr::null());
    assert!(test_fn_ptr3.inner().is_none());

    let r = std::panic::catch_unwind(move || {
        test_fn_ptr3();
    });

    assert!(r.is_err());

    assert_eq!(test_fn_ptr, test_fn_ptr3);
}

#[test]
#[cfg(feature = "fnptr")]
fn snd_test_function_pointer_equal() {
    let n = test_function as *const c_void;
    let test_fn_ptr = unsafe { send_fn_ptr_from_addr!(extern "C" fn() -> u64, n) };
    let test_fn_ptr2 = send_fn_ptr!(extern "C" fn() -> u64, test_function);

    //Compiler doesn't guarantee this...
    //assert_eq!(test_fn_ptr, test_fn_ptr2);
    //assert_eq!(test_fn_ptr.as_address(), test_fn_ptr2.as_address());

    if test_fn_ptr == test_fn_ptr2
        || test_fn_ptr.as_address() == test_fn_ptr2.as_address()
        || n as usize == test_function as usize
    {
        //But this is guaranteed
        assert_eq!(test_fn_ptr, test_fn_ptr2);
        assert_eq!(test_fn_ptr.as_address(), test_fn_ptr2.as_address());
    }

    let test_fn_ptr3 = send_fn_ptr!(extern "C" fn() -> u64, test_function2);
    assert_ne!(test_fn_ptr, test_fn_ptr3);
    assert_ne!(test_fn_ptr.as_address(), test_fn_ptr3.as_address());
}

#[test]
fn test_fmt() {
    unsafe {
        let mut memory = vec![0u8; 12345];
        let handle: *mut c_void = memory.as_mut_ptr().add(12345).cast();
        assert_eq!(
            format!("{:p}", handle.as_send_const()).as_str(),
            format!("{:p}", handle).as_str()
        );
        assert_eq!(
            format!("{:p}", handle.as_send_mut()).as_str(),
            format!("{:p}", handle).as_str()
        );
        assert_eq!(
            format!("{:p}", handle.as_sync_mut()).as_str(),
            format!("{:p}", handle).as_str()
        );
        assert_eq!(
            format!("{:p}", handle.as_sync_const()).as_str(),
            format!("{:p}", handle).as_str()
        );
        assert_ne!(
            format!("{:p}", handle.as_sync_const()).as_str(),
            format!("{:p}", memory.as_ptr()).as_str()
        );
    }
}

#[cfg(target_has_atomic = "32")]
#[test]
fn test_mt() {
    unsafe {
        let n = Box::into_raw(Box::new(123));
        let ptr = n.as_sync_mut();
        let jh = std::thread::spawn(move || {
            assert_eq!(123, ptr.read_volatile());
            ptr.write_volatile(456);
        });
        jh.join().unwrap();
        assert_eq!(n.read_volatile(), 456);
        _ = Box::from_raw(n);
    }
}