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
use crate::access::{Aligned, MaybeUnaligned};
use crate::get_api;
use crate::sys;
use crate::FromVariant;
use crate::ToVariant;
use crate::Variant;
use crate::VariantArray;
use crate::Vector3;

use std::mem::transmute;

/// A reference-counted vector of `Vector3` that uses Godot's pool allocator.
pub struct Vector3Array(pub(crate) sys::godot_pool_vector3_array);

pub type Read<'a> = Aligned<ReadGuard<'a>>;
pub type Write<'a> = Aligned<WriteGuard<'a>>;

impl Vector3Array {
    /// Creates an empty array.
    pub fn new() -> Self {
        Vector3Array::default()
    }

    /// Creates an array by trying to convert each variant.
    ///
    /// See `Variant::to_vector3`.
    pub fn from_variant_array(array: &VariantArray) -> Self {
        unsafe {
            let mut result = sys::godot_pool_vector3_array::default();
            (get_api().godot_pool_vector3_array_new_with_array)(&mut result, &array.0);
            Vector3Array(result)
        }
    }

    /// Appends a vector to the end of the array.
    pub fn push(&mut self, vector: &Vector3) {
        unsafe {
            (get_api().godot_pool_vector3_array_append)(&mut self.0, transmute(vector));
        }
    }

    /// Appends each vector to the end of the array.
    pub fn push_array(&mut self, vectors: &Vector3Array) {
        unsafe {
            (get_api().godot_pool_vector3_array_append_array)(&mut self.0, transmute(vectors));
        }
    }

    // TODO(error handling)
    /// Inserts a vector at the given offset.
    pub fn insert(&mut self, offset: i32, vector: &Vector3) -> bool {
        unsafe {
            let status =
                (get_api().godot_pool_vector3_array_insert)(&mut self.0, offset, transmute(vector));
            status != sys::godot_error_GODOT_OK
        }
    }

    /// Inverts the order of the elements in the array.
    pub fn invert(&mut self) {
        unsafe { (get_api().godot_pool_vector3_array_invert)(&mut self.0) }
    }

    /// Removes an element at the given offset.
    pub fn remove(&mut self, idx: i32) {
        unsafe {
            (get_api().godot_pool_vector3_array_remove)(&mut self.0, idx);
        }
    }

    /// Changes the size of the array, possibly removing elements or pushing default values.
    pub fn resize(&mut self, size: i32) {
        unsafe {
            (get_api().godot_pool_vector3_array_resize)(&mut self.0, size);
        }
    }

    /// Returns a copy of the element at the given offset.
    pub fn get(&self, idx: i32) -> Vector3 {
        unsafe { transmute((get_api().godot_pool_vector3_array_get)(&self.0, idx)) }
    }

    /// Sets the value of the element at the given offset.
    pub fn set(&mut self, idx: i32, vector: &Vector3) {
        unsafe {
            (get_api().godot_pool_vector3_array_set)(&mut self.0, idx, transmute(vector));
        }
    }

    /// Returns the number of elements in the array.
    pub fn len(&self) -> i32 {
        unsafe { (get_api().godot_pool_vector3_array_size)(&self.0) }
    }

    pub fn read<'a>(&'a self) -> Read<'a> {
        unsafe {
            MaybeUnaligned::new(ReadGuard::new(self.sys()))
                .try_into_aligned()
                .expect("Pool array access should be aligned. This indicates a bug in Godot")
        }
    }

    pub fn write<'a>(&'a mut self) -> Write<'a> {
        unsafe {
            MaybeUnaligned::new(WriteGuard::new(self.sys() as *mut _))
                .try_into_aligned()
                .expect("Pool array access should be aligned. This indicates a bug in Godot")
        }
    }

    #[doc(hidden)]
    pub fn sys(&self) -> *const sys::godot_pool_vector3_array {
        &self.0
    }

    #[doc(hidden)]
    pub fn from_sys(sys: sys::godot_pool_vector3_array) -> Self {
        Vector3Array(sys)
    }

    impl_common_methods! {
        /// Creates a new reference to this array.
        pub fn new_ref(&self) -> Vector3Array : godot_pool_vector3_array_new_copy;
    }
}

impl_basic_traits!(
    for Vector3Array as godot_pool_vector3_array {
        Drop => godot_pool_vector3_array_destroy;
        Default => godot_pool_vector3_array_new;
    }
);

impl ToVariant for Vector3Array {
    fn to_variant(&self) -> Variant {
        Variant::from_vector3_array(self)
    }
}

impl FromVariant for Vector3Array {
    fn from_variant(variant: &Variant) -> Option<Self> {
        variant.try_to_vector3_array()
    }
}

define_access_guard! {
    pub struct ReadGuard<'a> : sys::godot_pool_vector3_array_read_access {
        access = godot_pool_vector3_array_read(*const sys::godot_pool_vector3_array),
        len = godot_pool_vector3_array_size,
    }
    Guard<Target=Vector3> => godot_pool_vector3_array_read_access_ptr -> *const sys::godot_vector3;
    Drop => godot_pool_vector3_array_read_access_destroy;
    Clone => godot_pool_vector3_array_read_access_copy;
}

define_access_guard! {
    pub struct WriteGuard<'a> : sys::godot_pool_vector3_array_write_access {
        access = godot_pool_vector3_array_write(*mut sys::godot_pool_vector3_array),
        len = godot_pool_vector3_array_size,
    }
    Guard<Target=Vector3> + WritePtr => godot_pool_vector3_array_write_access_ptr -> *mut sys::godot_vector3;
    Drop => godot_pool_vector3_array_write_access_destroy;
}

godot_test!(
    test_vector3_array_access {
        let mut arr = Vector3Array::new();
        arr.push(&Vector3::new(1.0, 2.0, 3.0));
        arr.push(&Vector3::new(3.0, 4.0, 5.0));
        arr.push(&Vector3::new(5.0, 6.0, 7.0));

        let original_read = {
            let read = arr.read();
            assert_eq!(&[
                Vector3::new(1.0, 2.0, 3.0),
                Vector3::new(3.0, 4.0, 5.0),
                Vector3::new(5.0, 6.0, 7.0),
            ], read.as_slice());
            read.clone()
        };

        let mut cow_arr = arr.new_ref();

        {
            let mut write = cow_arr.write();
            assert_eq!(3, write.len());
            for s in write.as_mut_slice() {
                s.x += 2.0;
                s.y += 1.0;
            }
        }

        assert_eq!(Vector3::new(3.0, 3.0, 3.0), cow_arr.get(0));
        assert_eq!(Vector3::new(5.0, 5.0, 5.0), cow_arr.get(1));
        assert_eq!(Vector3::new(7.0, 7.0, 7.0), cow_arr.get(2));

        // the write shouldn't have affected the original array
        assert_eq!(&[
            Vector3::new(1.0, 2.0, 3.0),
            Vector3::new(3.0, 4.0, 5.0),
            Vector3::new(5.0, 6.0, 7.0),
        ], original_read.as_slice());
    }
);