alloy_primitives/
common.rsuse crate::Address;
#[cfg(feature = "rlp")]
use alloy_rlp::{Buf, BufMut, Decodable, Encodable, EMPTY_STRING_CODE};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "arbitrary", derive(derive_arbitrary::Arbitrary, proptest_derive::Arbitrary))]
#[doc(alias = "TransactionKind")]
pub enum TxKind {
#[default]
Create,
Call(Address),
}
impl From<Option<Address>> for TxKind {
#[inline]
fn from(value: Option<Address>) -> Self {
match value {
None => Self::Create,
Some(addr) => Self::Call(addr),
}
}
}
impl From<Address> for TxKind {
#[inline]
fn from(value: Address) -> Self {
Self::Call(value)
}
}
impl From<TxKind> for Option<Address> {
#[inline]
fn from(value: TxKind) -> Self {
value.to().copied()
}
}
impl TxKind {
pub const fn to(&self) -> Option<&Address> {
match self {
Self::Create => None,
Self::Call(to) => Some(to),
}
}
#[inline]
pub const fn is_create(&self) -> bool {
matches!(self, Self::Create)
}
#[inline]
pub const fn is_call(&self) -> bool {
matches!(self, Self::Call(_))
}
#[inline]
pub const fn size(&self) -> usize {
core::mem::size_of::<Self>()
}
}
#[cfg(feature = "rlp")]
impl Encodable for TxKind {
fn encode(&self, out: &mut dyn BufMut) {
match self {
Self::Call(to) => to.encode(out),
Self::Create => out.put_u8(EMPTY_STRING_CODE),
}
}
fn length(&self) -> usize {
match self {
Self::Call(to) => to.length(),
Self::Create => 1, }
}
}
#[cfg(feature = "rlp")]
impl Decodable for TxKind {
fn decode(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
if let Some(&first) = buf.first() {
if first == EMPTY_STRING_CODE {
buf.advance(1);
Ok(Self::Create)
} else {
let addr = <Address as Decodable>::decode(buf)?;
Ok(Self::Call(addr))
}
} else {
Err(alloy_rlp::Error::InputTooShort)
}
}
}
#[cfg(feature = "serde")]
impl serde::Serialize for TxKind {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
self.to().serialize(serializer)
}
}
#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for TxKind {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
Ok(Option::<Address>::deserialize(deserializer)?.into())
}
}