-
Notifications
You must be signed in to change notification settings - Fork 21
Simplify lifetimes #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -70,26 +70,28 @@ where | |
| } | ||
|
|
||
| /// Same as `to_bytes` but write directly into an `std::io::Write` object. | ||
| pub fn serialize_into<W, T>(write: &mut W, value: &T) -> Result<()> | ||
| pub fn serialize_into<T>(mut write: impl std::io::Write, value: &T) -> Result<()> | ||
| where | ||
| W: ?Sized + std::io::Write, | ||
| T: ?Sized + Serialize, | ||
|
Comment on lines
+73
to
75
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think losing the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mmm good question. Let me check
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no loss of functionality: |
||
| { | ||
| let serializer = Serializer::new(write, crate::MAX_CONTAINER_DEPTH); | ||
| let serializer = Serializer::new(&mut write, crate::MAX_CONTAINER_DEPTH); | ||
| value.serialize(serializer) | ||
| } | ||
|
|
||
| /// Same as `serialize_into` but use `limit` as max container depth instead of MAX_CONTAINER_DEPTH | ||
| /// Note that `limit` has to be lower than MAX_CONTAINER_DEPTH | ||
| pub fn serialize_into_with_limit<W, T>(write: &mut W, value: &T, limit: usize) -> Result<()> | ||
| pub fn serialize_into_with_limit<T>( | ||
| mut write: impl std::io::Write, | ||
|
Comment on lines
+83
to
+84
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah i would probably keep the generic bounds but we could have this just be
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same thing: We're basically replacing |
||
| value: &T, | ||
| limit: usize, | ||
| ) -> Result<()> | ||
| where | ||
| W: ?Sized + std::io::Write, | ||
| T: ?Sized + Serialize, | ||
| { | ||
| if limit > crate::MAX_CONTAINER_DEPTH { | ||
| return Err(Error::NotSupported("limit exceeds the max allowed depth")); | ||
| } | ||
| let serializer = Serializer::new(write, limit); | ||
| let serializer = Serializer::new(&mut write, limit); | ||
| value.serialize(serializer) | ||
| } | ||
|
|
||
|
|
@@ -140,17 +142,17 @@ pub fn is_human_readable() -> bool { | |
| } | ||
|
|
||
| /// Serialization implementation for BCS | ||
| struct Serializer<'a, W: ?Sized> { | ||
| output: &'a mut W, | ||
| struct Serializer<W> { | ||
| output: W, | ||
| max_remaining_depth: usize, | ||
| } | ||
|
|
||
| impl<'a, W> Serializer<'a, W> | ||
| impl<W> Serializer<W> | ||
| where | ||
| W: ?Sized + std::io::Write, | ||
| W: std::io::Write, | ||
| { | ||
| /// Creates a new `Serializer` which will emit BCS. | ||
| fn new(output: &'a mut W, max_remaining_depth: usize) -> Self { | ||
| fn new(output: W, max_remaining_depth: usize) -> Self { | ||
| Self { | ||
| output, | ||
| max_remaining_depth, | ||
|
|
@@ -190,17 +192,17 @@ where | |
| } | ||
| } | ||
|
|
||
| impl<'a, W> ser::Serializer for Serializer<'a, W> | ||
| impl<'a, W> ser::Serializer for Serializer<&'a mut W> | ||
| where | ||
| W: ?Sized + std::io::Write, | ||
| W: std::io::Write, | ||
| { | ||
| type Ok = (); | ||
| type Error = Error; | ||
| type SerializeSeq = Self; | ||
| type SerializeTuple = Self; | ||
| type SerializeTupleStruct = Self; | ||
| type SerializeTupleVariant = Self; | ||
| type SerializeMap = MapSerializer<'a, W>; | ||
| type SerializeMap = MapSerializer<&'a mut W>; | ||
| type SerializeStruct = Self; | ||
| type SerializeStructVariant = Self; | ||
|
|
||
|
|
@@ -403,9 +405,9 @@ where | |
| } | ||
| } | ||
|
|
||
| impl<'a, W> ser::SerializeSeq for Serializer<'a, W> | ||
| impl<W> ser::SerializeSeq for Serializer<&mut W> | ||
| where | ||
| W: ?Sized + std::io::Write, | ||
| W: std::io::Write, | ||
| { | ||
| type Ok = (); | ||
| type Error = Error; | ||
|
|
@@ -414,17 +416,17 @@ where | |
| where | ||
| T: ?Sized + Serialize, | ||
| { | ||
| value.serialize(Serializer::new(self.output, self.max_remaining_depth)) | ||
| value.serialize(Serializer::new(&mut *self.output, self.max_remaining_depth)) | ||
| } | ||
|
|
||
| fn end(self) -> Result<()> { | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| impl<'a, W> ser::SerializeTuple for Serializer<'a, W> | ||
| impl<W> ser::SerializeTuple for Serializer<&mut W> | ||
| where | ||
| W: ?Sized + std::io::Write, | ||
| W: std::io::Write, | ||
| { | ||
| type Ok = (); | ||
| type Error = Error; | ||
|
|
@@ -433,17 +435,17 @@ where | |
| where | ||
| T: ?Sized + Serialize, | ||
| { | ||
| value.serialize(Serializer::new(self.output, self.max_remaining_depth)) | ||
| value.serialize(Serializer::new(&mut *self.output, self.max_remaining_depth)) | ||
| } | ||
|
|
||
| fn end(self) -> Result<()> { | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| impl<'a, W> ser::SerializeTupleStruct for Serializer<'a, W> | ||
| impl<W> ser::SerializeTupleStruct for Serializer<&mut W> | ||
| where | ||
| W: ?Sized + std::io::Write, | ||
| W: std::io::Write, | ||
| { | ||
| type Ok = (); | ||
| type Error = Error; | ||
|
|
@@ -452,17 +454,17 @@ where | |
| where | ||
| T: ?Sized + Serialize, | ||
| { | ||
| value.serialize(Serializer::new(self.output, self.max_remaining_depth)) | ||
| value.serialize(Serializer::new(&mut *self.output, self.max_remaining_depth)) | ||
| } | ||
|
|
||
| fn end(self) -> Result<()> { | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| impl<'a, W> ser::SerializeTupleVariant for Serializer<'a, W> | ||
| impl<W> ser::SerializeTupleVariant for Serializer<&mut W> | ||
| where | ||
| W: ?Sized + std::io::Write, | ||
| W: std::io::Write, | ||
| { | ||
| type Ok = (); | ||
| type Error = Error; | ||
|
|
@@ -471,7 +473,7 @@ where | |
| where | ||
| T: ?Sized + Serialize, | ||
| { | ||
| value.serialize(Serializer::new(self.output, self.max_remaining_depth)) | ||
| value.serialize(Serializer::new(&mut *self.output, self.max_remaining_depth)) | ||
| } | ||
|
|
||
| fn end(self) -> Result<()> { | ||
|
|
@@ -480,14 +482,14 @@ where | |
| } | ||
|
|
||
| #[doc(hidden)] | ||
| struct MapSerializer<'a, W: ?Sized> { | ||
| serializer: Serializer<'a, W>, | ||
| pub struct MapSerializer<W> { | ||
| serializer: Serializer<W>, | ||
| entries: Vec<(Vec<u8>, Vec<u8>)>, | ||
| next_key: Option<Vec<u8>>, | ||
| } | ||
|
|
||
| impl<'a, W: ?Sized> MapSerializer<'a, W> { | ||
| fn new(serializer: Serializer<'a, W>) -> Self { | ||
| impl<W> MapSerializer<W> { | ||
| fn new(serializer: Serializer<W>) -> Self { | ||
| MapSerializer { | ||
| serializer, | ||
| entries: Vec::new(), | ||
|
|
@@ -496,9 +498,9 @@ impl<'a, W: ?Sized> MapSerializer<'a, W> { | |
| } | ||
| } | ||
|
|
||
| impl<'a, W> ser::SerializeMap for MapSerializer<'a, W> | ||
| impl<W> ser::SerializeMap for MapSerializer<W> | ||
| where | ||
| W: ?Sized + std::io::Write, | ||
| W: std::io::Write, | ||
| { | ||
| type Ok = (); | ||
| type Error = Error; | ||
|
|
@@ -557,9 +559,9 @@ where | |
| } | ||
| } | ||
|
|
||
| impl<'a, W> ser::SerializeStruct for Serializer<'a, W> | ||
| impl<W> ser::SerializeStruct for Serializer<&mut W> | ||
| where | ||
| W: ?Sized + std::io::Write, | ||
| W: std::io::Write, | ||
| { | ||
| type Ok = (); | ||
| type Error = Error; | ||
|
|
@@ -568,17 +570,17 @@ where | |
| where | ||
| T: ?Sized + Serialize, | ||
| { | ||
| value.serialize(Serializer::new(self.output, self.max_remaining_depth)) | ||
| value.serialize(Serializer::new(&mut *self.output, self.max_remaining_depth)) | ||
| } | ||
|
|
||
| fn end(self) -> Result<()> { | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| impl<'a, W> ser::SerializeStructVariant for Serializer<'a, W> | ||
| impl<W> ser::SerializeStructVariant for Serializer<&mut W> | ||
| where | ||
| W: ?Sized + std::io::Write, | ||
| W: std::io::Write, | ||
| { | ||
| type Ok = (); | ||
| type Error = Error; | ||
|
|
@@ -587,7 +589,7 @@ where | |
| where | ||
| T: ?Sized + Serialize, | ||
| { | ||
| value.serialize(Serializer::new(self.output, self.max_remaining_depth)) | ||
| value.serialize(Serializer::new(&mut *self.output, self.max_remaining_depth)) | ||
| } | ||
|
|
||
| fn end(self) -> Result<()> { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not something that we really need to deal with right now but it would be interesting if we could restructure this crate to be able to operate in
no_stdenvironments since afaik the only usage ofstdwe really have today is via thestd::iotraitsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Someone submitted this PR: #15