diff src/libpam/memory.rs @ 141:a508a69c068a

Remove a lot of Results from functions. Many functions are documented to only return failing Results when given improper inputs or when there is a memory allocation failure (which can be verified by looking at the source). In cases where we know our input is correct, we don't need to check for memory allocation errors for the same reason that Rust doesn't do so when you, e.g., create a new Vec.
author Paul Fisher <paul@pfish.zone>
date Sat, 05 Jul 2025 17:16:56 -0400
parents 33b9622ed6d2
children ebb71a412b58
line wrap: on
line diff
--- a/src/libpam/memory.rs	Sat Jul 05 17:11:33 2025 -0400
+++ b/src/libpam/memory.rs	Sat Jul 05 17:16:56 2025 -0400
@@ -57,11 +57,11 @@
 #[allow(clippy::wrong_self_convention)]
 impl<T> CHeapBox<T> {
     /// Creates a new CHeapBox holding the given data.
-    pub fn new(value: T) -> Result<Self> {
+    pub fn new(value: T) -> Self {
         let memory = calloc(1);
         unsafe { ptr::write(memory.as_ptr(), value) }
         // SAFETY: We literally just allocated this.
-        Ok(Self(memory))
+        Self(memory)
     }
 
     /// Takes ownership of the given pointer.
@@ -95,7 +95,8 @@
     ///
     /// # Safety
     ///
-    /// The different type has to be compatible in size/alignment and drop behavior.
+    /// The other type has to have the same size and alignment and
+    /// have compatible drop behavior with respect to other resources.
     pub unsafe fn cast<R>(this: Self) -> CHeapBox<R> {
         mem::transmute(this)
     }
@@ -103,7 +104,7 @@
 
 impl<T: Default> Default for CHeapBox<T> {
     fn default() -> Self {
-        Self::new(Default::default()).expect("allocation should not fail")
+        Self::new(Default::default())
     }
 }
 
@@ -274,10 +275,10 @@
             }
         }
 
-        let mut dropbox = CHeapBox::new(Dropper(&drop_count)).unwrap();
+        let mut dropbox = CHeapBox::new(Dropper(&drop_count));
         _ = dropbox;
         // ensure the old value is dropped when the new one is assigned.
-        dropbox = CHeapBox::new(Dropper(&drop_count)).unwrap();
+        dropbox = CHeapBox::new(Dropper(&drop_count));
         assert_eq!(1, drop_count.get());
         *dropbox = Dropper(&drop_count);
         assert_eq!(2, drop_count.get());