]> git.lizzy.rs Git - rust.git/commitdiff
refactor: VecDeques IterMut fields to private
authorDeveloperC <DeveloperC@protonmail.com>
Sat, 25 Sep 2021 12:55:45 +0000 (13:55 +0100)
committerDeveloperC <DeveloperC@protonmail.com>
Tue, 5 Oct 2021 18:09:49 +0000 (19:09 +0100)
Made the fields of VecDeque's IterMut private by creating a IterMut::new(...) function to create a new instance of IterMut and migrating usage to use IterMut::new(...).

library/alloc/src/collections/vec_deque/iter_mut.rs
library/alloc/src/collections/vec_deque/mod.rs

index 7700b31cf5b469538f20e88cf652bc4223f2ac2d..31e6e3b06af5fdd1e7e27ee88b013db480d0283d 100644 (file)
 #[stable(feature = "rust1", since = "1.0.0")]
 pub struct IterMut<'a, T: 'a> {
     // Internal safety invariant: the entire slice is dereferencable.
-    pub(crate) ring: *mut [T],
-    pub(crate) tail: usize,
-    pub(crate) head: usize,
-    pub(crate) phantom: PhantomData<&'a mut [T]>,
+    ring: *mut [T],
+    tail: usize,
+    head: usize,
+    phantom: PhantomData<&'a mut [T]>,
+}
+
+impl<'a, T> IterMut<'a, T> {
+    pub(super) unsafe fn new(
+        ring: *mut [T],
+        tail: usize,
+        head: usize,
+        phantom: PhantomData<&'a mut [T]>,
+    ) -> Self {
+        IterMut { ring, tail, head, phantom }
+    }
 }
 
 // SAFETY: we do nothing thread-local and there is no interior mutability,
index f4de2b2ebe5dc1afdef8378827fa5b440b5b97ab..54fcb358ede1be7cc5f3b4f864ca8b120a23d714 100644 (file)
@@ -1000,12 +1000,9 @@ pub fn iter(&self) -> Iter<'_, T> {
     pub fn iter_mut(&mut self) -> IterMut<'_, T> {
         // SAFETY: The internal `IterMut` safety invariant is established because the
         // `ring` we create is a dereferencable slice for lifetime '_.
-        IterMut {
-            tail: self.tail,
-            head: self.head,
-            ring: ptr::slice_from_raw_parts_mut(self.ptr(), self.cap()),
-            phantom: PhantomData,
-        }
+        let ring = ptr::slice_from_raw_parts_mut(self.ptr(), self.cap());
+
+        unsafe { IterMut::new(ring, self.tail, self.head, PhantomData) }
     }
 
     /// Returns a pair of slices which contain, in order, the contents of the
@@ -1192,12 +1189,9 @@ pub fn range_mut<R>(&mut self, range: R) -> IterMut<'_, T>
 
         // SAFETY: The internal `IterMut` safety invariant is established because the
         // `ring` we create is a dereferencable slice for lifetime '_.
-        IterMut {
-            tail,
-            head,
-            ring: ptr::slice_from_raw_parts_mut(self.ptr(), self.cap()),
-            phantom: PhantomData,
-        }
+        let ring = ptr::slice_from_raw_parts_mut(self.ptr(), self.cap());
+
+        unsafe { IterMut::new(ring, tail, head, PhantomData) }
     }
 
     /// Creates a draining iterator that removes the specified range in the