]> git.nega.tv - josh/narcissus/commitdiff
Tidy some lints and documentation
authorJoshua Simmons <josh@nega.tv>
Fri, 11 Nov 2022 23:50:53 +0000 (00:50 +0100)
committerJoshua Simmons <josh@nega.tv>
Fri, 11 Nov 2022 23:50:53 +0000 (00:50 +0100)
narcissus-core/src/fixed_vec.rs
narcissus-core/src/manual_arc.rs
narcissus-core/src/rand.rs
narcissus-gpu/src/lib.rs
narcissus-gpu/src/vulkan.rs

index 7de170f4fd8c4528f697171c5cd4aa1a0f630a65..c9d33044116a81ee3aaf93222b244c8c3508e61f 100644 (file)
@@ -96,7 +96,7 @@ impl<T, const CAP: usize> FixedVec<T, CAP> {
     ///
     /// # Safety
     ///
-    /// - `new_len` must be less than or equal to [`capacity()`].
+    /// - `new_len` must be less than or equal to [`Self::capacity()`].
     /// - The elements at `old_len..new_len` must be initialized.
     #[inline]
     pub unsafe fn set_len(&mut self, new_len: usize) {
index 1ab61b414f90ee594e49c92daac334474355809f..ea168bb09bc09dfa85d858e61e971f1f11050522 100644 (file)
@@ -43,13 +43,14 @@ impl<T> Inner<T> {
 
 /// A thread-safe reference-counting pointer with manual management.
 ///
-/// The type [`ManualArc<T>`] provides shared ownership of a value of type T, allocated in the heap. Invoking clone on
-/// [`ManualArc`] produces a new [`ManualArc`] instance, which points to the same allocation on the heap as the source
-/// [`ManualArc`], while increasing a reference count.
+/// The type [`ManualArc<T>`] provides shared ownership of a value of type T, allocated in the heap.
+/// Invoking clone on [`ManualArc`] produces a new [`ManualArc`] instance, which points to the same
+/// allocation as the source, while increasing a reference count.
 ///
-/// Before a [`ManualArc`] is dropped, the [`ManualArc::release`] method must be called. [`ManualArc::release`] will
-/// return the variant [`Release::Shared`] if there are other references outstanding, or [`Release::Unique`] with the
-/// object being released if the release operation removes the last reference.
+/// Before a [`ManualArc`] is dropped, the [`ManualArc::release`] method must be called.
+/// [`ManualArc::release`] will return the variant [`Release::Shared`] if there are other references
+/// outstanding, or [`Release::Unique`] with the contained object if the release operation removes
+/// the final reference.
 pub struct ManualArc<T> {
     ptr: Option<NonNull<Inner<T>>>,
     phantom: PhantomData<Inner<T>>,
@@ -68,21 +69,24 @@ impl<T> ManualArc<T> {
         }
     }
 
-    /// Returns the variant [`Release::Shared`] if there are other [`ManualArc`] objects alive that reference the
-    /// object that this instance does, or [`Release::Unique`] with the previously contained object if the release
-    /// operation removes the last reference.
+    /// Consumes `self`, decrementing the reference count.
     ///
-    /// Must be explicitly called to drop [`ManualArc`] instances. Dropping implicitly or explicitly without calling
-    /// this function will panic.
+    /// Returns the variant [`Release::Shared`] if there are other [`ManualArc`] instances still
+    /// holding references to the same object, or [`Release::Unique`] with the previously contained
+    /// object if the release operation removes the last reference.
+    ///
+    /// Must be explicitly called to drop [`ManualArc`] instances. Dropping implicitly or explicitly
+    /// without calling this function will panic.
     pub fn release(mut self) -> Release<T> {
         #[cold]
         #[inline(never)]
         unsafe fn release_slow<T>(ptr: NonNull<Inner<T>>) -> T {
-            // Ref-counting operations imply a full memory barrier on x86, but not in general. So insert an acquire
-            // barrier on the slow path here to ensure all modifications to inner are visible before we call drop.
+            // Ref-counting operations imply a full memory barrier on x86, but not in general. So
+            // insert an acquire barrier on the slow path to ensure all modifications to inner are
+            // visible before we call drop.
             std::sync::atomic::fence(Ordering::Acquire);
 
-            // Safety: Was created by Box::leak in the constructor, so it's valid to re-create a box here.
+            // Safety: Was created by Box::leak in the constructor, so it's valid to recreate a box.
             let mut inner = Box::from_raw(ptr.as_ptr());
             // extract the value from the container so we can return it.
             let value = ManuallyDrop::take(&mut inner.value);
@@ -92,8 +96,9 @@ impl<T> ManualArc<T> {
             value
         }
 
-        // Safety: `release` consumes self, so it's impossible to call twice. Therefore ptr is always valid since
-        // `release` is the only thing which invalidates it.
+        // Safety: `release` consumes `self` so it's impossible to call twice on the same instance,
+        // release is also the only function able to invalidate the pointer. Hence the pointer is
+        // always valid here.
         unsafe {
             // Replace ptr with None so that the drop function doesn't panic
             let ptr = std::mem::replace(&mut self.ptr, None);
@@ -102,8 +107,8 @@ impl<T> ManualArc<T> {
             if inner.decr_strong() {
                 Release::Shared
             } else {
-                // We have released the last reference to this inner, so we need to free it and return the contained
-                // value.
+                // We have released the last reference to this inner, so we need to free it and
+                // return the contained value.
                 let value = release_slow(ptr);
                 Release::Unique(value)
             }
@@ -119,11 +124,13 @@ impl<T: Default> Default for ManualArc<T> {
 
 impl<T> Clone for ManualArc<T> {
     fn clone(&self) -> Self {
-        // Safety: Inner is valid whilever we have a valid [`ManualArc`] outside of the [`ManualArc::release`] function.
-        let ptr = unsafe { self.ptr.unwrap_unchecked() };
-        let inner = unsafe { ptr.as_ref() };
-        inner.incr_strong();
-        Self::from_inner(ptr)
+        // Safety: Inner is valid whilever we have a valid `ManualArc`, and so long as we are outside
+        // the `release` function.
+        unsafe {
+            let ptr = self.ptr.unwrap_unchecked();
+            ptr.as_ref().incr_strong();
+            Self::from_inner(ptr)
+        }
     }
 }
 
@@ -138,7 +145,8 @@ impl<T> Drop for ManualArc<T> {
 impl<T> Deref for ManualArc<T> {
     type Target = T;
 
-    // Safety: Inner is valid whilever we have a valid [`ManualArc`] outside of the [`ManualArc::release`] function.
+    // Safety: Inner is valid whilever we have a valid `ManualArc`, and so long as we are outside
+    // the `release` function.
     #[inline(always)]
     fn deref(&self) -> &Self::Target {
         unsafe { self.ptr.unwrap_unchecked().as_ref() }
index e262acf18890e8c31ca6f7dab8c18c6885f12ba1..e6eccbe054ee4ef0b108012778c98dab2c4a09e6 100644 (file)
@@ -33,7 +33,7 @@ impl Pcg64 {
 
     /// Generates a uniformly distributed random number in the range `0..upper_bound`
     ///
-    /// Based on https://github.com/apple/swift/pull/39143/commits/87b3f607042e653a42b505442cc803ec20319c1c
+    /// Based on <https://github.com/apple/swift/pull/39143/commits/87b3f607042e653a42b505442cc803ec20319c1c>
     #[inline]
     #[must_use]
     pub fn next_bound_u64(&mut self, upper_bound: u64) -> u64 {
index b235c2e5e6832c73eacec8e53ea02c9fccf4e51d..86263f1213428a11ab72a19e4942e2e11b3b416c 100644 (file)
@@ -274,7 +274,19 @@ pub trait Device {
     );
     fn destroy_pipeline(&self, frame_token: &FrameToken, pipeline: Pipeline);
 
+    /// Map the given buffer in its entirety to system memory and return a pointer to it.
+    ///
+    /// # Safety
+    ///
+    /// `buffer` must be host mappable.
     unsafe fn map_buffer(&self, buffer: Buffer) -> *mut u8;
+
+    /// Unmap from system memory a buffer previously mapped.
+    ///
+    /// # Safety
+    ///
+    /// This will invalidate the pointer returned previously from `map_buffer`, so there must not be
+    /// any remaining references derived from that address.
     unsafe fn unmap_buffer(&self, buffer: Buffer);
 
     fn acquire_swapchain(
index 3633a1fe0402bbc4856f3d85eacfc430bcc84528..76413c74664538ef49a58787664b882d4f347385 100644 (file)
@@ -642,7 +642,7 @@ impl<'app> VulkanDevice<'app> {
                 }
             });
 
-            let descriptor_pool_pools = GpuConcurrent::new(|| vk::DescriptorPool::null());
+            let descriptor_pool_pools = GpuConcurrent::new(vk::DescriptorPool::null);
 
             UnsafeCell::new(VulkanFrame {
                 command_buffer_pools,