discogs_api/
parsed_response.rs

1use crate::Error;
2use serde::Deserialize;
3use std::ops::Deref;
4
5/// Wrapper for deserialized data.
6///
7/// # Safety
8/// This contains a raw pointer to the underlying bytes of the response used to get the data.
9/// However, this is only used on drop to deallocate it (after which there are no valid references
10/// to `data`) and cannot be constructed such that it can be invalidated at drop.
11pub struct ParsedResponse<T> {
12    b: *mut [u8],
13    data: T,
14}
15
16impl<T: std::fmt::Debug> std::fmt::Debug for ParsedResponse<T> {
17    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
18        f.write_str(&format!("{:?}", self.data))
19    }
20}
21
22impl<T> Drop for ParsedResponse<T> {
23    fn drop(&mut self) {
24        unsafe {
25            drop(Box::from_raw(self.b));
26        }
27    }
28}
29
30impl<T> Deref for ParsedResponse<T> {
31    type Target = T;
32    fn deref(&self) -> &Self::Target {
33        &self.data
34    }
35}
36
37impl<'de, T: Deserialize<'de>> ParsedResponse<T> {
38    pub(crate) fn new(resp: reqwest::blocking::Response) -> Result<Self, Error> {
39        let b: *mut [u8] = Box::leak(Box::from(resp.bytes()?.as_ref())) as *mut [u8];
40        let data = serde_json::from_slice(unsafe { b.as_ref().unwrap() })?;
41        Ok(Self { b, data })
42    }
43}