comparison 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
comparison
equal deleted inserted replaced
140:add7228adb2f 141:a508a69c068a
55 55
56 // Lots of "as" and "into" associated functions. 56 // Lots of "as" and "into" associated functions.
57 #[allow(clippy::wrong_self_convention)] 57 #[allow(clippy::wrong_self_convention)]
58 impl<T> CHeapBox<T> { 58 impl<T> CHeapBox<T> {
59 /// Creates a new CHeapBox holding the given data. 59 /// Creates a new CHeapBox holding the given data.
60 pub fn new(value: T) -> Result<Self> { 60 pub fn new(value: T) -> Self {
61 let memory = calloc(1); 61 let memory = calloc(1);
62 unsafe { ptr::write(memory.as_ptr(), value) } 62 unsafe { ptr::write(memory.as_ptr(), value) }
63 // SAFETY: We literally just allocated this. 63 // SAFETY: We literally just allocated this.
64 Ok(Self(memory)) 64 Self(memory)
65 } 65 }
66 66
67 /// Takes ownership of the given pointer. 67 /// Takes ownership of the given pointer.
68 /// 68 ///
69 /// # Safety 69 /// # Safety
93 93
94 /// Converts this into a Box of a different type. 94 /// Converts this into a Box of a different type.
95 /// 95 ///
96 /// # Safety 96 /// # Safety
97 /// 97 ///
98 /// The different type has to be compatible in size/alignment and drop behavior. 98 /// The other type has to have the same size and alignment and
99 /// have compatible drop behavior with respect to other resources.
99 pub unsafe fn cast<R>(this: Self) -> CHeapBox<R> { 100 pub unsafe fn cast<R>(this: Self) -> CHeapBox<R> {
100 mem::transmute(this) 101 mem::transmute(this)
101 } 102 }
102 } 103 }
103 104
104 impl<T: Default> Default for CHeapBox<T> { 105 impl<T: Default> Default for CHeapBox<T> {
105 fn default() -> Self { 106 fn default() -> Self {
106 Self::new(Default::default()).expect("allocation should not fail") 107 Self::new(Default::default())
107 } 108 }
108 } 109 }
109 110
110 impl Buffer for CHeapBox<u8> { 111 impl Buffer for CHeapBox<u8> {
111 fn allocate(len: usize) -> Self { 112 fn allocate(len: usize) -> Self {
272 fn drop(&mut self) { 273 fn drop(&mut self) {
273 self.0.set(self.0.get() + 1) 274 self.0.set(self.0.get() + 1)
274 } 275 }
275 } 276 }
276 277
277 let mut dropbox = CHeapBox::new(Dropper(&drop_count)).unwrap(); 278 let mut dropbox = CHeapBox::new(Dropper(&drop_count));
278 _ = dropbox; 279 _ = dropbox;
279 // ensure the old value is dropped when the new one is assigned. 280 // ensure the old value is dropped when the new one is assigned.
280 dropbox = CHeapBox::new(Dropper(&drop_count)).unwrap(); 281 dropbox = CHeapBox::new(Dropper(&drop_count));
281 assert_eq!(1, drop_count.get()); 282 assert_eq!(1, drop_count.get());
282 *dropbox = Dropper(&drop_count); 283 *dropbox = Dropper(&drop_count);
283 assert_eq!(2, drop_count.get()); 284 assert_eq!(2, drop_count.get());
284 drop(dropbox); 285 drop(dropbox);
285 assert_eq!(3, drop_count.get()); 286 assert_eq!(3, drop_count.get());