Skip to content

Commit e3e6f7f

Browse files
authored
feat: requestpacket helpers (#2304)
1 parent e1821db commit e3e6f7f

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

Diff for: crates/json-rpc/src/packet.rs

+16
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,22 @@ impl RequestPacket {
4747
Self::Batch(Vec::with_capacity(capacity))
4848
}
4949

50+
/// Returns the [`SerializedRequest`] if this packet is [`ResponsePacket::Single`]
51+
pub const fn as_single(&self) -> Option<&SerializedRequest> {
52+
match self {
53+
Self::Single(req) => Some(req),
54+
Self::Batch(_) => None,
55+
}
56+
}
57+
58+
/// Returns the batch of [`SerializedRequest`] if this packet is [`ResponsePacket::Batch`]
59+
pub fn as_batch(&self) -> Option<&[SerializedRequest]> {
60+
match self {
61+
Self::Batch(req) => Some(req.as_slice()),
62+
Self::Single(_) => None,
63+
}
64+
}
65+
5066
/// Serialize the packet as a boxed [`RawValue`].
5167
pub fn serialize(self) -> serde_json::Result<Box<RawValue>> {
5268
match self {

0 commit comments

Comments
 (0)