matrixmultiply/
ptr.rs

1// Copyright 2020, 2022 Ulrik Sverdrup "bluss"
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9use rawpointer::PointerExt;
10
11/// A Send + Sync raw pointer wrapper
12#[derive(Copy, Clone)]
13#[repr(transparent)]
14pub(crate) struct Ptr<T> { ptr: T }
15unsafe impl<T> Sync for Ptr<*const T> { }
16unsafe impl<T> Sync for Ptr<*mut T> { }
17unsafe impl<T> Send for Ptr<*const T> { }
18unsafe impl<T> Send for Ptr<*mut T> { }
19
20/// Create a Ptr
21///
22/// # Safety
23///
24/// Unsafe since it is thread safety critical to use the raw pointer correctly.
25#[allow(non_snake_case)]
26pub(crate) unsafe fn Ptr<T>(ptr: T) -> Ptr<T> { Ptr { ptr } }
27
28impl<T> Ptr<T> {
29    /// Get the pointer
30    pub(crate) fn ptr(self) -> T
31        where T: Copy
32    {
33        self.ptr
34    }
35}
36
37impl<T> Ptr<*mut T> {
38    /// Get as *const T
39    pub(crate) fn to_const(self) -> Ptr<*const T> {
40        Ptr { ptr: self.ptr }
41    }
42}
43
44impl<T> PointerExt for Ptr<*const T> {
45    #[inline(always)]
46    unsafe fn offset(self, i: isize) -> Self {
47        Ptr(self.ptr.offset(i))
48    }
49}
50
51impl<T> PointerExt for Ptr<*mut T> {
52    #[inline(always)]
53    unsafe fn offset(self, i: isize) -> Self {
54        Ptr(self.ptr.offset(i))
55    }
56}