Skip to content
This repository has been archived by the owner on Jul 2, 2024. It is now read-only.

Commit

Permalink
Tidy up code style
Browse files Browse the repository at this point in the history
  • Loading branch information
SanmerDev committed Feb 8, 2024
1 parent b30b7ce commit 2d464c2
Showing 1 changed file with 60 additions and 60 deletions.
120 changes: 60 additions & 60 deletions src/ser.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::error;
use crate::error::Result;
use serde::{ser, Serialize};
use serde::ser;
use std::io;
use std::io::Write;

Expand Down Expand Up @@ -30,17 +30,17 @@ impl<W: Write> Serializer<W> {

pub trait Formatter {
#[inline]
fn write_null<W>(&mut self, writer: &mut W) -> io::Result<()>
fn write_null<W: ?Sized>(&mut self, writer: &mut W) -> io::Result<()>
where
W: ?Sized + Write,
W: Write,
{
writer.write_all(b"")
}

#[inline]
fn write_bool<W>(&mut self, writer: &mut W, value: bool) -> io::Result<()>
fn write_bool<W: ?Sized>(&mut self, writer: &mut W, value: bool) -> io::Result<()>
where
W: ?Sized + Write,
W: Write,
{
let s = if value {
b"true" as &[u8]
Expand All @@ -51,141 +51,141 @@ pub trait Formatter {
}

#[inline]
fn write_i8<W>(&mut self, writer: &mut W, value: i8) -> io::Result<()>
fn write_i8<W: ?Sized>(&mut self, writer: &mut W, value: i8) -> io::Result<()>
where
W: ?Sized + Write,
W: Write,
{
let mut buffer = itoa::Buffer::new();
let s = buffer.format(value);
writer.write_all(s.as_bytes())
}

#[inline]
fn write_i16<W>(&mut self, writer: &mut W, value: i16) -> io::Result<()>
fn write_i16<W: ?Sized>(&mut self, writer: &mut W, value: i16) -> io::Result<()>
where
W: ?Sized + Write,
W: Write,
{
let mut buffer = itoa::Buffer::new();
let s = buffer.format(value);
writer.write_all(s.as_bytes())
}

#[inline]
fn write_i32<W>(&mut self, writer: &mut W, value: i32) -> io::Result<()>
fn write_i32<W: ?Sized>(&mut self, writer: &mut W, value: i32) -> io::Result<()>
where
W: ?Sized + Write,
W: Write,
{
let mut buffer = itoa::Buffer::new();
let s = buffer.format(value);
writer.write_all(s.as_bytes())
}

#[inline]
fn write_i64<W>(&mut self, writer: &mut W, value: i64) -> io::Result<()>
fn write_i64<W: ?Sized>(&mut self, writer: &mut W, value: i64) -> io::Result<()>
where
W: ?Sized + Write,
W: Write,
{
let mut buffer = itoa::Buffer::new();
let s = buffer.format(value);
writer.write_all(s.as_bytes())
}

#[inline]
fn write_u8<W>(&mut self, writer: &mut W, value: u8) -> io::Result<()>
fn write_u8<W: ?Sized>(&mut self, writer: &mut W, value: u8) -> io::Result<()>
where
W: ?Sized + Write,
W: Write,
{
let mut buffer = itoa::Buffer::new();
let s = buffer.format(value);
writer.write_all(s.as_bytes())
}

#[inline]
fn write_u16<W>(&mut self, writer: &mut W, value: u16) -> io::Result<()>
fn write_u16<W: ?Sized>(&mut self, writer: &mut W, value: u16) -> io::Result<()>
where
W: ?Sized + Write,
W: Write,
{
let mut buffer = itoa::Buffer::new();
let s = buffer.format(value);
writer.write_all(s.as_bytes())
}

#[inline]
fn write_u32<W>(&mut self, writer: &mut W, value: u32) -> io::Result<()>
fn write_u32<W: ?Sized>(&mut self, writer: &mut W, value: u32) -> io::Result<()>
where
W: ?Sized + Write,
W: Write,
{
let mut buffer = itoa::Buffer::new();
let s = buffer.format(value);
writer.write_all(s.as_bytes())
}

#[inline]
fn write_u64<W>(&mut self, writer: &mut W, value: u64) -> io::Result<()>
fn write_u64<W: ?Sized>(&mut self, writer: &mut W, value: u64) -> io::Result<()>
where
W: ?Sized + Write,
W: Write,
{
let mut buffer = itoa::Buffer::new();
let s = buffer.format(value);
writer.write_all(s.as_bytes())
}

#[inline]
fn write_f32<W>(&mut self, writer: &mut W, value: f32) -> io::Result<()>
fn write_f32<W: ?Sized>(&mut self, writer: &mut W, value: f32) -> io::Result<()>
where
W: ?Sized + Write,
W: Write,
{
let mut buffer = ryu::Buffer::new();
let s = buffer.format_finite(value);
writer.write_all(s.as_bytes())
}

#[inline]
fn write_f64<W>(&mut self, writer: &mut W, value: f64) -> io::Result<()>
fn write_f64<W: ?Sized>(&mut self, writer: &mut W, value: f64) -> io::Result<()>
where
W: ?Sized + Write,
W: Write,
{
let mut buffer = ryu::Buffer::new();
let s = buffer.format_finite(value);
writer.write_all(s.as_bytes())
}

#[inline]
fn write_str<W>(&mut self, writer: &mut W, value: &str) -> io::Result<()>
fn write_str<W: ?Sized>(&mut self, writer: &mut W, value: &str) -> io::Result<()>
where
W: ?Sized + Write,
W: Write,
{
writer.write_all(value.as_bytes())
}

#[inline]
fn begin_key<W>(&mut self, writer: &mut W) -> io::Result<()>
fn begin_key<W: ?Sized>(&mut self, writer: &mut W) -> io::Result<()>
where
W: ?Sized + Write,
W: Write,
{
writer.write_all(b"\n")
}

#[inline]
fn end_key<W>(&mut self, writer: &mut W) -> io::Result<()>
fn end_key<W: ?Sized>(&mut self, writer: &mut W) -> io::Result<()>
where
W: ?Sized + Write,
W: Write,
{
writer.write_all(b"=")
}

#[inline]
fn begin_value<W>(&mut self, writer: &mut W) -> io::Result<()>
fn begin_value<W: ?Sized>(&mut self, writer: &mut W) -> io::Result<()>
where
W: ?Sized + Write,
W: Write,
{
writer.write_all(b"")
}

#[inline]
fn end_value<W>(&mut self, writer: &mut W) -> io::Result<()>
fn end_value<W: ?Sized>(&mut self, writer: &mut W) -> io::Result<()>
where
W: ?Sized + Write,
W: Write,
{
writer.write_all(b"")
}
Expand Down Expand Up @@ -289,7 +289,7 @@ impl<'a, W: Write, F: Formatter> ser::Serializer for &'a mut Serializer<W, F> {

fn serialize_some<T: ?Sized>(self, value: &T) -> Result<Self::Ok>
where
T: Serialize,
T: ser::Serialize,
{
value.serialize(self)
}
Expand Down Expand Up @@ -319,7 +319,7 @@ impl<'a, W: Write, F: Formatter> ser::Serializer for &'a mut Serializer<W, F> {
_value: &T,
) -> Result<Self::Ok>
where
T: Serialize,
T: ser::Serialize,
{
Err(ser::Error::custom("unsupported newtype struct"))
}
Expand All @@ -332,7 +332,7 @@ impl<'a, W: Write, F: Formatter> ser::Serializer for &'a mut Serializer<W, F> {
_value: &T,
) -> Result<Self::Ok>
where
T: Serialize,
T: ser::Serialize,
{
Err(ser::Error::custom("unsupported newtype variant"))
}
Expand Down Expand Up @@ -401,9 +401,9 @@ impl<'a, W: Write, F: Formatter> ser::SerializeSeq for Compound<'a, W, F> {
type Ok = ();
type Error = error::Error;

fn serialize_element<T>(&mut self, _value: &T) -> Result<Self::Ok>
fn serialize_element<T: ?Sized>(&mut self, _value: &T) -> Result<Self::Ok>
where
T: ?Sized + Serialize,
T: ser::Serialize,
{
unimplemented!()
}
Expand All @@ -417,9 +417,9 @@ impl<'a, W: Write, F: Formatter> ser::SerializeTuple for Compound<'a, W, F> {
type Ok = ();
type Error = error::Error;

fn serialize_element<T>(&mut self, _value: &T) -> Result<Self::Ok>
fn serialize_element<T: ?Sized>(&mut self, _value: &T) -> Result<Self::Ok>
where
T: ?Sized + Serialize,
T: ser::Serialize,
{
unimplemented!()
}
Expand All @@ -433,9 +433,9 @@ impl<'a, W: Write, F: Formatter> ser::SerializeTupleStruct for Compound<'a, W, F
type Ok = ();
type Error = error::Error;

fn serialize_field<T>(&mut self, _value: &T) -> Result<Self::Ok>
fn serialize_field<T: ?Sized>(&mut self, _value: &T) -> Result<Self::Ok>
where
T: ?Sized + Serialize,
T: ser::Serialize,
{
unimplemented!()
}
Expand All @@ -449,9 +449,9 @@ impl<'a, W: Write, F: Formatter> ser::SerializeTupleVariant for Compound<'a, W,
type Ok = ();
type Error = error::Error;

fn serialize_field<T>(&mut self, _value: &T) -> Result<Self::Ok>
fn serialize_field<T: ?Sized>(&mut self, _value: &T) -> Result<Self::Ok>
where
T: ?Sized + Serialize,
T: ser::Serialize,
{
unimplemented!()
}
Expand All @@ -465,9 +465,9 @@ impl<'a, W: Write, F: Formatter> ser::SerializeMap for Compound<'a, W, F> {
type Ok = ();
type Error = error::Error;

fn serialize_key<T>(&mut self, key: &T) -> Result<Self::Ok>
fn serialize_key<T: ?Sized>(&mut self, key: &T) -> Result<Self::Ok>
where
T: ?Sized + Serialize,
T: ser::Serialize,
{
match self {
Compound::Map { ser, first } => {
Expand All @@ -487,9 +487,9 @@ impl<'a, W: Write, F: Formatter> ser::SerializeMap for Compound<'a, W, F> {
}
}

fn serialize_value<T>(&mut self, value: &T) -> Result<Self::Ok>
fn serialize_value<T: ?Sized>(&mut self, value: &T) -> Result<Self::Ok>
where
T: ?Sized + Serialize,
T: ser::Serialize,
{
match self {
Compound::Map { ser, .. } => {
Expand All @@ -514,9 +514,9 @@ impl<'a, W: Write, F: Formatter> ser::SerializeStruct for Compound<'a, W, F> {
type Ok = ();
type Error = error::Error;

fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<Self::Ok>
fn serialize_field<T: ?Sized>(&mut self, key: &'static str, value: &T) -> Result<Self::Ok>
where
T: ?Sized + Serialize,
T: ser::Serialize,
{
match self {
Compound::Map { .. } => ser::SerializeMap::serialize_entry(self, key, value),
Expand All @@ -532,9 +532,9 @@ impl<'a, W: Write, F: Formatter> ser::SerializeStructVariant for Compound<'a, W,
type Ok = ();
type Error = error::Error;

fn serialize_field<T>(&mut self, _key: &'static str, _value: &T) -> Result<Self::Ok>
fn serialize_field<T: ?Sized>(&mut self, _key: &'static str, _value: &T) -> Result<Self::Ok>
where
T: ?Sized + Serialize,
T: ser::Serialize,
{
unimplemented!()
}
Expand All @@ -545,29 +545,29 @@ impl<'a, W: Write, F: Formatter> ser::SerializeStructVariant for Compound<'a, W,
}

#[inline]
pub fn to_writer<W, T>(writer: W, value: &T) -> Result<()>
pub fn to_writer<W, T: ?Sized>(writer: W, value: &T) -> Result<()>
where
W: Write,
T: ?Sized + Serialize,
T: ser::Serialize,
{
let mut ser = Serializer::new(writer);
value.serialize(&mut ser)
}

#[inline]
pub fn to_vec<T>(value: &T) -> Result<Vec<u8>>
pub fn to_vec<T: ?Sized>(value: &T) -> Result<Vec<u8>>
where
T: ?Sized + Serialize,
T: ser::Serialize,
{
let mut writer = Vec::with_capacity(128);
to_writer(&mut writer, value)?;
Ok(writer)
}

#[inline]
pub fn to_string<T>(value: &T) -> Result<String>
pub fn to_string<T: ?Sized>(value: &T) -> Result<String>
where
T: ?Sized + Serialize,
T: ser::Serialize,
{
let vec = to_vec(value)?;
let string = unsafe { String::from_utf8_unchecked(vec) };
Expand Down

0 comments on commit 2d464c2

Please sign in to comment.