Last active
February 13, 2024 22:21
-
-
Save Alxandr/1fc481ab1451bbbbef1a8d8bdeeec8f6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pub trait Deserialize<T, D: Fallible + ?Sized> { | |
/// Deserializes using the given deserializer | |
fn deserialize(&self, deserializer: &mut D) -> Result<T, D::Error>; | |
#[inline] | |
fn deserialize_into(&self, deserializer: &mut D, target: *mut Self) -> Result<(), D::Error> { | |
let result = self.deserialize(deserializer)?; | |
ptr.write(result); | |
Ok(()) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[inline] | |
unsafe fn deserialize_unsized( | |
&self, | |
deserializer: &mut D, | |
mut alloc: impl FnMut(Layout) -> *mut u8, | |
) -> Result<*mut (), D::Error> { | |
let layout = Layout::new::<T>(); | |
if layout.size() == 0 { | |
let _ = self.deserialize(deserializer)?; | |
Ok(ptr::NonNull::<T>::dangling().as_ptr().cast()) | |
} else { | |
let ptr = alloc(layout).cast::<T>(); | |
assert!(!ptr.is_null()); | |
self.deserialize_into(deserializer, ptr)?; | |
Ok(ptr.cast()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment