-
-
Save as-com/9cc3c8f9de5358f891764f184d8cb354 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
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements | |
--> src\testcase.rs:119:26 | |
| | |
119 | seed.deserialize(D::from_input(self.input)).map(Some) | |
| ^^^^^^^^^^^^^ | |
| | |
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 108:5... | |
--> src\testcase.rs:108:5 | |
| | |
108 | fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error> where T: DeserializeSeed<'de> { | |
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | |
note: ...so that reference does not outlive borrowed content | |
--> src\testcase.rs:119:40 | |
| | |
119 | seed.deserialize(D::from_input(self.input)).map(Some) | |
| ^^^^^^^^^^ | |
note: but, the lifetime must be valid for the lifetime `'a` as defined on the impl at 105:11... | |
--> src\testcase.rs:105:11 | |
| | |
105 | impl<'de, 'a, D: DeserializerWithInput<'de, 'a>> SeqAccess<'de> for ArraySeqAccess<'de, 'a, D> { | |
| ^^ | |
note: ...so that the types are compatible | |
--> src\testcase.rs:119:26 | |
| | |
119 | seed.deserialize(D::from_input(self.input)).map(Some) | |
| ^^^^^^^^^^^^^ | |
= note: expected `testcase::DeserializerWithInput<'_, '_>` | |
found `testcase::DeserializerWithInput<'de, 'a>` | |
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
use serde::Deserializer; | |
use std::fmt::{Display, Formatter}; | |
use serde::forward_to_deserialize_any; | |
use serde::de::{Visitor, SeqAccess, DeserializeSeed}; | |
use std::marker::PhantomData; | |
#[derive(Debug)] | |
pub enum Error { | |
Something(String) | |
} | |
impl std::error::Error for Error {} | |
impl Display for Error { | |
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | |
write!(f, "{:?}", self) | |
} | |
} | |
impl serde::de::Error for Error { | |
fn custom<T>(msg: T) -> Self where | |
T: Display { | |
Error::Something(msg.to_string()) | |
} | |
} | |
pub trait DeserializerWithInput<'de, 'a>: Deserializer<'de, Error=Error> { | |
fn from_input(input: &'a mut &'de [u8]) -> Self; | |
} | |
pub struct IntDeserializer<'de, 'a> { | |
input: &'a mut &'de [u8], | |
} | |
impl<'de, 'a> Deserializer<'de> for IntDeserializer<'de, 'a> { | |
type Error = Error; | |
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> where | |
V: Visitor<'de> { | |
let x = self.input[0]; | |
*self.input = &self.input[1..]; // pretend this actually does something useful | |
visitor.visit_u8(x) | |
} | |
forward_to_deserialize_any! { | |
bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string | |
bytes byte_buf option unit unit_struct newtype_struct seq tuple | |
tuple_struct map struct enum identifier ignored_any | |
} | |
} | |
impl<'de, 'a> DeserializerWithInput<'de, 'a> for IntDeserializer<'de, 'a> { | |
fn from_input(input: &'a mut &'de [u8]) -> Self { | |
IntDeserializer { | |
input | |
} | |
} | |
} | |
pub struct ArrayDeserializer<'de, 'a, D: DeserializerWithInput<'de, 'a>> { | |
pub input: &'a mut &'de [u8], | |
deserializer: PhantomData<D>, | |
} | |
impl<'de, 'a, D: DeserializerWithInput<'de, 'a>> DeserializerWithInput<'de, 'a> for ArrayDeserializer<'de, 'a, D> { | |
fn from_input(input: &'a mut &'de [u8]) -> Self { | |
ArrayDeserializer { | |
input, | |
deserializer: PhantomData, | |
} | |
} | |
} | |
impl<'de, 'a, D: DeserializerWithInput<'de, 'a>> Deserializer<'de> for ArrayDeserializer<'de, 'a, D> { | |
type Error = Error; | |
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> where | |
V: Visitor<'de> { | |
visitor.visit_seq(<ArraySeqAccess<IntDeserializer>>::new(self.input)) | |
} | |
forward_to_deserialize_any! { | |
bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string | |
bytes byte_buf option unit unit_struct newtype_struct seq tuple | |
tuple_struct map struct enum identifier ignored_any | |
} | |
} | |
pub struct ArraySeqAccess<'de, 'a, D: DeserializerWithInput<'de, 'a>> { | |
input: &'a mut &'de [u8], | |
remaining_in_chunk: i64, | |
deserializer: PhantomData<D>, | |
} | |
impl<'de, 'a, D: DeserializerWithInput<'de, 'a>> ArraySeqAccess<'de, 'a, D> { | |
pub fn new(input: &'a mut &'de [u8]) -> Self { | |
ArraySeqAccess { | |
input, | |
remaining_in_chunk: 0, | |
deserializer: PhantomData, | |
} | |
} | |
} | |
impl<'de, 'a, D: DeserializerWithInput<'de, 'a>> SeqAccess<'de> for ArraySeqAccess<'de, 'a, D> { | |
type Error = Error; | |
fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error> where T: DeserializeSeed<'de> { | |
if self.remaining_in_chunk == 0 { | |
let mut chunk_size = self.input[0]; // again pretend this actually does something | |
*self.input = &self.input[1..]; | |
if chunk_size == 0 { | |
return Ok(None); | |
} | |
self.remaining_in_chunk = chunk_size as i64; | |
} | |
self.remaining_in_chunk -= 1; | |
seed.deserialize(D::from_input(self.input)).map(Some) | |
} | |
} | |
fn foo() { | |
let mut input: &[u8] = &[5, 5, 5, 5, 5, 5, 0]; | |
<ArrayDeserializer<IntDeserializer>>::from_input(&mut input); // called by Serde's machinery | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment