Skip to content

Commit

Permalink
chore(json-abi): avoid unsafe, remove unused generics (#229)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes authored Aug 13, 2023
1 parent 5f39812 commit d54c105
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
3 changes: 1 addition & 2 deletions crates/json-abi/src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,7 @@ impl JsonAbi {
macro_rules! next_item {
($self:ident; $($ident:ident.$f:ident()),* $(,)?) => {$(
if let Some(next) = $self.$ident.$f() {
// SAFETY: length is valid
$self.len = unsafe { $self.len.checked_sub(1).unwrap_unchecked() };
$self.len -= 1;
return Some(next.into())
}
)*};
Expand Down
36 changes: 22 additions & 14 deletions crates/json-abi/src/param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,12 @@ pub struct Param {

impl fmt::Display for Param {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(internal_type) = &self.internal_type {
write!(f, "{} ", internal_type)?;
}
if let Some(it) = &self.internal_type {
it.fmt(f)
} else {
f.write_str(&self.ty)
}?;
f.write_str(" ")?;
f.write_str(&self.name)
}
}
Expand All @@ -69,6 +72,7 @@ impl<'de> Deserialize<'de> for Param {
}

impl Serialize for Param {
#[inline]
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
self.as_inner().serialize(serializer)
}
Expand Down Expand Up @@ -195,12 +199,13 @@ impl Param {
}
}

#[inline]
fn borrowed_internal_type(&self) -> Option<BorrowedInternalType<'_>> {
self.internal_type().as_ref().map(|it| it.as_borrowed())
}

#[inline]
fn as_inner(&self) -> BorrowedParam<'_, Param> {
fn as_inner(&self) -> BorrowedParam<'_> {
BorrowedParam {
name: &self.name,
ty: &self.ty,
Expand Down Expand Up @@ -241,9 +246,12 @@ pub struct EventParam {

impl fmt::Display for EventParam {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(internal_type) = &self.internal_type {
write!(f, "{} ", internal_type)?;
}
if let Some(it) = &self.internal_type {
it.fmt(f)
} else {
f.write_str(&self.ty)
}?;
f.write_str(" ")?;
f.write_str(&self.name)
}
}
Expand Down Expand Up @@ -271,6 +279,7 @@ impl<'de> Deserialize<'de> for EventParam {
}

impl Serialize for EventParam {
#[inline]
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
self.as_inner().serialize(serializer)
}
Expand Down Expand Up @@ -397,12 +406,13 @@ impl EventParam {
}
}

#[inline]
fn borrowed_internal_type(&self) -> Option<BorrowedInternalType<'_>> {
self.internal_type().as_ref().map(|it| it.as_borrowed())
}

#[inline]
fn as_inner(&self) -> BorrowedParam<'_, Param> {
fn as_inner(&self) -> BorrowedParam<'_> {
BorrowedParam {
name: &self.name,
ty: &self.ty,
Expand All @@ -413,9 +423,8 @@ impl EventParam {
}
}

#[derive(Deserialize, Serialize, Debug)]
#[serde(bound(deserialize = "<[T] as ToOwned>::Owned: Default + Deserialize<'de>"))]
struct BorrowedParam<'a, T: Clone> {
#[derive(Deserialize, Serialize)]
struct BorrowedParam<'a> {
name: &'a str,
#[serde(rename = "type")]
ty: &'a str,
Expand All @@ -424,12 +433,11 @@ struct BorrowedParam<'a, T: Clone> {
#[serde(
rename = "internalType",
default,
skip_serializing_if = "Option::is_none",
borrow
skip_serializing_if = "Option::is_none"
)]
internal_type: Option<BorrowedInternalType<'a>>,
#[serde(default, skip_serializing_if = "<[_]>::is_empty")]
components: Cow<'a, [T]>,
components: Cow<'a, [Param]>,
}

#[cfg(test)]
Expand Down

0 comments on commit d54c105

Please sign in to comment.