Skip to main content

luaur_common/methods/
vec_deque_operator_assign_vec_deque.rs

1use crate::records::vec_deque::VecDeque;
2use core::ptr::NonNull;
3
4impl<T> VecDeque<T> {
5    #[allow(non_snake_case)]
6    pub fn operator_assign(&mut self, other: &VecDeque<T>) -> &mut Self {
7        if core::ptr::eq(self, other) {
8            return self;
9        }
10
11        // destroy all of the existing elements
12        self.destroyElements();
13
14        if self.buffer_capacity < other.queue_size {
15            // free the current buffer
16            self.deallocate(self.buffer, self.buffer_capacity);
17
18            self.buffer = Some(self.allocate(other.buffer_capacity));
19            self.buffer_capacity = other.buffer_capacity;
20        }
21
22        let head_size = core::cmp::min(other.queue_size, other.buffer_capacity - other.head);
23        let tail_size = other.queue_size - head_size;
24
25        // Assignment doesn't try to match the capacity of 'other' and thus makes the buffer contiguous
26        self.head = 0;
27        self.queue_size = other.queue_size;
28
29        if let Some(dst_ptr) = self.buffer {
30            if let Some(src_ptr) = other.buffer {
31                unsafe {
32                    if head_size != 0 {
33                        core::ptr::copy_nonoverlapping(
34                            src_ptr.as_ptr().add(other.head),
35                            dst_ptr.as_ptr(),
36                            head_size,
37                        );
38                    }
39
40                    if tail_size != 0 {
41                        core::ptr::copy_nonoverlapping(
42                            src_ptr.as_ptr(),
43                            dst_ptr.as_ptr().add(head_size),
44                            tail_size,
45                        );
46                    }
47                }
48            }
49        }
50
51        self
52    }
53}