Skip to content

Commit

Permalink
bye bye stry
Browse files Browse the repository at this point in the history
Signed-off-by: Heinz N. Gies <[email protected]>
  • Loading branch information
Licenser committed Aug 23, 2024
1 parent 6d197e2 commit 21cc445
Show file tree
Hide file tree
Showing 10 changed files with 182 additions and 248 deletions.
13 changes: 0 additions & 13 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1298,19 +1298,6 @@ macro_rules! static_cast_u64 {
};
}

/// FROM serde-json
/// We only use our own error type; no need for From conversions provided by the
/// standard library's try! macro. This reduces lines of LLVM IR by 4%.
#[macro_export]
macro_rules! stry {
($e:expr) => {
match $e {
::std::result::Result::Ok(val) => val,
::std::result::Result::Err(err) => return ::std::result::Result::Err(err),
}
};
}

#[cfg(test)]
mod test {
use crate::prelude::*;
Expand Down
17 changes: 7 additions & 10 deletions src/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ mod se;
mod value;
pub use self::se::*;
pub use self::value::*;
use crate::{stry, Buffers, Deserializer, Error, ErrorType, Node, Result};
use crate::{BorrowedValue, OwnedValue};
use crate::{Buffers, Deserializer, Error, ErrorType, Node, Result};
use serde::de::DeserializeOwned;
use serde_ext::Deserialize;
use std::fmt;
Expand Down Expand Up @@ -57,7 +57,7 @@ pub fn from_slice<'a, T>(s: &'a mut [u8]) -> Result<T>
where
T: Deserialize<'a>,
{
let mut deserializer = stry!(Deserializer::from_slice(s));
let mut deserializer = Deserializer::from_slice(s)?;
T::deserialize(&mut deserializer)
}

Expand All @@ -74,7 +74,7 @@ pub fn from_slice_with_buffers<'a, T>(s: &'a mut [u8], buffers: &mut Buffers) ->
where
T: Deserialize<'a>,
{
let mut deserializer = stry!(Deserializer::from_slice_with_buffers(s, buffers));
let mut deserializer = Deserializer::from_slice_with_buffers(s, buffers)?;
T::deserialize(&mut deserializer)
}

Expand All @@ -98,7 +98,7 @@ pub unsafe fn from_str<'a, T>(s: &'a mut str) -> Result<T>
where
T: Deserialize<'a>,
{
let mut deserializer = stry!(Deserializer::from_slice(s.as_bytes_mut()));
let mut deserializer = Deserializer::from_slice(s.as_bytes_mut())?;

T::deserialize(&mut deserializer)
}
Expand All @@ -125,10 +125,7 @@ pub unsafe fn from_str_with_buffers<'a, T>(s: &'a mut str, buffers: &mut Buffers
where
T: Deserialize<'a>,
{
let mut deserializer = stry!(Deserializer::from_slice_with_buffers(
s.as_bytes_mut(),
buffers
));
let mut deserializer = Deserializer::from_slice_with_buffers(s.as_bytes_mut(), buffers)?;

T::deserialize(&mut deserializer)
}
Expand All @@ -149,7 +146,7 @@ where
if let Err(e) = rdr.read_to_end(&mut data) {
return Err(Error::generic(ErrorType::Io(e)));
};
let mut deserializer = stry!(Deserializer::from_slice(&mut data));
let mut deserializer = Deserializer::from_slice(&mut data)?;
T::deserialize(&mut deserializer)
}

Expand All @@ -171,7 +168,7 @@ where
if let Err(e) = rdr.read_to_end(&mut data) {
return Err(Error::generic(ErrorType::Io(e)));
};
let mut deserializer = stry!(Deserializer::from_slice_with_buffers(&mut data, buffers));
let mut deserializer = Deserializer::from_slice_with_buffers(&mut data, buffers)?;
T::deserialize(&mut deserializer)
}

Expand Down
42 changes: 21 additions & 21 deletions src/serde/de.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::serde_ext::de::IntoDeserializer;
use crate::{stry, Deserializer, Error, ErrorType, Node, Result, StaticNode};
use crate::{Deserializer, Error, ErrorType, Node, Result, StaticNode};
use serde_ext::de::{self, DeserializeSeed, MapAccess, SeqAccess, Visitor};
use serde_ext::forward_to_deserialize_any;
use std::str;
Expand All @@ -18,7 +18,7 @@ where
where
V: Visitor<'de>,
{
match stry!(self.next()) {
match self.next()? {
Node::String(s) => visitor.visit_borrowed_str(s),
Node::Static(StaticNode::Null) => visitor.visit_unit(),
Node::Static(StaticNode::Bool(b)) => visitor.visit_bool(b),
Expand Down Expand Up @@ -53,7 +53,7 @@ where
where
V: Visitor<'de>,
{
match stry!(self.next()) {
match self.next()? {
Node::Static(StaticNode::Bool(b)) => visitor.visit_bool(b),
_c => Err(Deserializer::error(ErrorType::ExpectedBoolean)),
}
Expand Down Expand Up @@ -93,7 +93,7 @@ where
where
V: Visitor<'de>,
{
visitor.visit_i8(stry!(self.parse_i8()))
visitor.visit_i8(self.parse_i8()?)
}

#[cfg_attr(not(feature = "no-inline"), inline)]
Expand All @@ -102,7 +102,7 @@ where
where
V: Visitor<'de>,
{
visitor.visit_i16(stry!(self.parse_i16()))
visitor.visit_i16(self.parse_i16()?)
}

#[cfg_attr(not(feature = "no-inline"), inline)]
Expand All @@ -111,23 +111,23 @@ where
where
V: Visitor<'de>,
{
visitor.visit_i32(stry!(self.parse_i32()))
visitor.visit_i32(self.parse_i32()?)
}

#[cfg_attr(not(feature = "no-inline"), inline)]
fn deserialize_i64<V>(self, visitor: V) -> Result<V::Value>
where
V: Visitor<'de>,
{
visitor.visit_i64(stry!(self.parse_i64()))
visitor.visit_i64(self.parse_i64()?)
}

#[cfg_attr(not(feature = "no-inline"), inline)]
fn deserialize_i128<V>(self, visitor: V) -> Result<V::Value>
where
V: Visitor<'de>,
{
visitor.visit_i128(stry!(self.parse_i128()))
visitor.visit_i128(self.parse_i128()?)
}

#[cfg_attr(not(feature = "no-inline"), inline)]
Expand All @@ -136,7 +136,7 @@ where
where
V: Visitor<'de>,
{
visitor.visit_u8(stry!(self.parse_u8()))
visitor.visit_u8(self.parse_u8()?)
}

#[cfg_attr(not(feature = "no-inline"), inline)]
Expand All @@ -145,7 +145,7 @@ where
where
V: Visitor<'de>,
{
visitor.visit_u16(stry!(self.parse_u16()))
visitor.visit_u16(self.parse_u16()?)
}

#[cfg_attr(not(feature = "no-inline"), inline)]
Expand All @@ -154,23 +154,23 @@ where
where
V: Visitor<'de>,
{
visitor.visit_u32(stry!(self.parse_u32()))
visitor.visit_u32(self.parse_u32()?)
}

#[cfg_attr(not(feature = "no-inline"), inline)]
fn deserialize_u64<V>(self, visitor: V) -> Result<V::Value>
where
V: Visitor<'de>,
{
visitor.visit_u64(stry!(self.parse_u64()))
visitor.visit_u64(self.parse_u64()?)
}

#[cfg_attr(not(feature = "no-inline"), inline)]
fn deserialize_u128<V>(self, visitor: V) -> Result<V::Value>
where
V: Visitor<'de>,
{
visitor.visit_u128(stry!(self.parse_u128()))
visitor.visit_u128(self.parse_u128()?)
}

#[cfg_attr(not(feature = "no-inline"), inline)]
Expand All @@ -179,7 +179,7 @@ where
where
V: Visitor<'de>,
{
let v: f64 = stry!(self.parse_double());
let v: f64 = self.parse_double()?;
visitor.visit_f32(v as f32)
}

Expand All @@ -188,7 +188,7 @@ where
where
V: Visitor<'de>,
{
visitor.visit_f64(stry!(self.parse_double()))
visitor.visit_f64(self.parse_double()?)
}

// An absent optional is represented as the JSON `null` and a present
Expand All @@ -205,7 +205,7 @@ where
where
V: Visitor<'de>,
{
if stry!(self.peek()) == Node::Static(StaticNode::Null) {
if self.peek()? == Node::Static(StaticNode::Null) {
self.skip();
visitor.visit_unit()
} else {
Expand All @@ -219,7 +219,7 @@ where
where
V: Visitor<'de>,
{
if stry!(self.next()) != Node::Static(StaticNode::Null) {
if self.next()? != Node::Static(StaticNode::Null) {
return Err(Deserializer::error(ErrorType::ExpectedNull));
}
visitor.visit_unit()
Expand Down Expand Up @@ -371,7 +371,7 @@ impl<'de, 'a> de::EnumAccess<'de> for VariantAccess<'a, 'de> {
where
V: de::DeserializeSeed<'de>,
{
let val = stry!(seed.deserialize(&mut *self.de));
let val = seed.deserialize(&mut *self.de)?;
Ok((val, self))
}
}
Expand Down Expand Up @@ -487,12 +487,12 @@ macro_rules! deserialize_integer_key {
where
V: de::Visitor<'de>,
{
visitor.$visit(stry!(match unsafe { self.de.next_() } {
visitor.$visit(match unsafe { self.de.next_() } {
Node::String(s) => s
.parse::<$type>()
.map_err(|_| Deserializer::error(ErrorType::InvalidNumber)),
_ => Err(Deserializer::error(ErrorType::ExpectedString)),
}))
}?)
}
};
}
Expand All @@ -505,7 +505,7 @@ impl<'de, 'a> de::Deserializer<'de> for MapKey<'de, 'a> {
where
V: de::Visitor<'de>,
{
match stry!(self.de.next()) {
match self.de.next()? {
Node::String(s) => visitor.visit_borrowed_str(s),
_ => Err(Deserializer::error(ErrorType::ExpectedString)),
}
Expand Down
4 changes: 2 additions & 2 deletions src/serde/se/pp.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{stry, Error, ErrorType};
use crate::{Error, ErrorType};
use serde_ext::ser;
use std::io::Write;
use std::str;
Expand Down Expand Up @@ -87,7 +87,7 @@ where
9 => self.get_writer().write_all(b" "),
_ => {
for _ in 0..(self.dent * 2) {
stry!(self.get_writer().write_all(b" "));
self.get_writer().write_all(b" ")?;
}
Ok(())
}
Expand Down
19 changes: 9 additions & 10 deletions src/serde/value/borrowed/se.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use super::to_value;
use crate::{
cow::Cow,
stry,
value::borrowed::{Object, Value},
Error, ErrorType, Result,
};
Expand Down Expand Up @@ -192,7 +191,7 @@ impl<'se> serde::Serializer for Serializer<'se> {
T: ?Sized + Serialize,
{
let mut values = Object::with_capacity_and_hasher(1, ObjectHasher::default());
let x = stry!(to_value(value));
let x = to_value(value)?;
values.insert_nocheck(variant.into(), x);
Ok(Value::from(values))
}
Expand Down Expand Up @@ -293,7 +292,7 @@ impl<'se> serde::ser::SerializeSeq for SerializeVec<'se> {
where
T: ?Sized + Serialize,
{
self.vec.push(stry!(to_value(value)));
self.vec.push(to_value(value)?);
Ok(())
}

Expand Down Expand Up @@ -342,7 +341,7 @@ impl<'se> serde::ser::SerializeTupleVariant for SerializeTupleVariant<'se> {
where
T: ?Sized + Serialize,
{
self.vec.push(stry!(to_value(value)));
self.vec.push(to_value(value)?);
Ok(())
}

Expand All @@ -362,9 +361,9 @@ impl<'se> serde::ser::SerializeMap for SerializeMap<'se> {
where
T: ?Sized + Serialize,
{
self.next_key = Some(stry!(key.serialize(MapKeySerializer {
marker: PhantomData
})));
self.next_key = Some(key.serialize(MapKeySerializer {
marker: PhantomData,
})?);
Ok(())
}

Expand All @@ -376,7 +375,7 @@ impl<'se> serde::ser::SerializeMap for SerializeMap<'se> {
// Panic because this indicates a bug in the program rather than an
// expected failure.
let key = key.expect("serialize_value called before serialize_key");
self.map.insert(key, stry!(to_value(value)));
self.map.insert(key, to_value(value)?);
Ok(())
}

Expand Down Expand Up @@ -570,7 +569,7 @@ impl<'se> serde::ser::SerializeStruct for SerializeMap<'se> {
where
T: ?Sized + Serialize,
{
stry!(serde::ser::SerializeMap::serialize_key(self, key));
serde::ser::SerializeMap::serialize_key(self, key)?;
serde::ser::SerializeMap::serialize_value(self, value)
}

Expand All @@ -587,7 +586,7 @@ impl<'se> serde::ser::SerializeStructVariant for SerializeStructVariant<'se> {
where
T: ?Sized + Serialize,
{
self.map.insert(key.into(), stry!(to_value(value)));
self.map.insert(key.into(), to_value(value)?);
Ok(())
}

Expand Down
Loading

0 comments on commit 21cc445

Please sign in to comment.