Skip to content

Commit

Permalink
into_static for serde_sv2 primitives
Browse files Browse the repository at this point in the history
  • Loading branch information
lorbax committed Jun 6, 2024
1 parent 39517ad commit 9e48228
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 10 deletions.
18 changes: 18 additions & 0 deletions protocols/v2/binary-sv2/serde-sv2/src/primitives/sequences/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::{ShortTxId, Signature, B016M, B0255, B064K, U24, U256};
use crate::Error;
use alloc::vec::Vec;
use core::convert::TryInto;
use serde::{de::Visitor, Serialize};

Expand All @@ -22,6 +23,23 @@ struct Seq<'s, T: Clone + Serialize + TryFromBSlice<'s>> {
_a: core::marker::PhantomData<T>,
}

impl<'s, T: Clone + Serialize + TryFromBSlice<'s>> Seq<'s, T> {
pub fn parse(self) -> Result<Vec<T>, ()> {
let mut ret = Vec::with_capacity(self.size as usize);
let elem_len = self.size as usize;
let mut first = 0;
let mut last = elem_len;
while last <= self.data.len() {
let slice = &self.data[first..last];
let elem = T::try_from_slice(slice).map_err(|_| ())?;
ret.push(elem);
first += elem_len;
last += elem_len;
}
Ok(ret)
}
}

struct SeqVisitor<T> {
inner_type_size: u8,
max_len: SeqMaxLen,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,15 @@ impl<'s> Sv2Option<'s, U256<'s>> {
data: Some(data),
}
} else {
panic!()
// this is an already valid seq should be safe to call the unwraps.
// also this library shouldn't be used for priduction envs so is ok do thigs like this
// one
let data = self.seq.unwrap().parse().unwrap();
let data = data.into_iter().map(|i| i.into_static()).collect();
Sv2Option {
seq: None,
data: Some(data),
}
}
}
pub fn inner_as_ref(&self) -> &[&[u8]] {
Expand All @@ -254,7 +262,14 @@ impl<'s> Sv2Option<'s, u32> {
data: Some(inner),
}
} else {
panic!()
// this is an already valid seq should be safe to call the unwraps.
// also this library shouldn't be used for priduction envs so is ok do thigs like this
// one
let data = self.seq.unwrap().parse().unwrap();
Sv2Option {
seq: None,
data: Some(data),
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,15 @@ impl<'s> Seq0255<'s, U256<'s>> {
data: Some(data),
}
} else {
panic!()
// this is an already valid seq should be safe to call the unwraps.
// also this library shouldn't be used for priduction envs so is ok do thigs like this
// one
let data = self.seq.unwrap().parse().unwrap();
let data = data.into_iter().map(|i| i.into_static()).collect();
Seq0255 {
seq: None,
data: Some(data),
}
}
}
pub fn inner_as_ref(&self) -> &[&[u8]] {
Expand All @@ -258,7 +266,14 @@ impl<'s> Seq0255<'s, u32> {
data: Some(inner),
}
} else {
panic!()
// this is an already valid seq should be safe to call the unwraps.
// also this library shouldn't be used for priduction envs so is ok do thigs like this
// one
let data = self.seq.unwrap().parse().unwrap();
Seq0255 {
seq: None,
data: Some(data),
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,15 @@ impl<'s> Seq064K<'s, B064K<'s>> {
data: Some(data),
}
} else {
panic!()
// this is an already valid seq should be safe to call the unwraps.
// also this library shouldn't be used for priduction envs so is ok do thigs like this
// one
let data = self.seq.unwrap().parse().unwrap();
let data = data.into_iter().map(|i| i.into_static()).collect();
Seq064K {
seq: None,
data: Some(data),
}
}
}
}
Expand All @@ -466,7 +474,15 @@ impl<'s> Seq064K<'s, B016M<'s>> {
data: Some(data),
}
} else {
panic!()
// this is an already valid seq should be safe to call the unwraps.
// also this library shouldn't be used for priduction envs so is ok do thigs like this
// one
let data = self.seq.unwrap().parse().unwrap();
let data = data.into_iter().map(|i| i.into_static()).collect();
Seq064K {
seq: None,
data: Some(data),
}
}
}
pub fn to_vec(&self) -> Vec<Vec<u8>> {
Expand All @@ -486,7 +502,14 @@ impl<'s> Seq064K<'s, u32> {
data: Some(inner),
}
} else {
panic!()
// this is an already valid seq should be safe to call the unwraps.
// also this library shouldn't be used for priduction envs so is ok do thigs like this
// one
let data = self.seq.unwrap().parse().unwrap();
Seq064K {
seq: None,
data: Some(data),
}
}
}
}
Expand All @@ -498,7 +521,14 @@ impl<'s> Seq064K<'s, u16> {
data: Some(inner),
}
} else {
panic!()
// this is an already valid seq should be safe to call the unwraps.
// also this library shouldn't be used for priduction envs so is ok do thigs like this
// one
let data = self.seq.unwrap().parse().unwrap();
Seq064K {
seq: None,
data: Some(data),
}
}
}
}
Expand All @@ -513,7 +543,15 @@ impl<'s> Seq064K<'s, ShortTxId<'s>> {
data: Some(data),
}
} else {
panic!()
// this is an already valid seq should be safe to call the unwraps.
// also this library shouldn't be used for priduction envs so is ok do thigs like this
// one
let data = self.seq.unwrap().parse().unwrap();
let data = data.into_iter().map(|i| i.into_static()).collect();
Seq064K {
seq: None,
data: Some(data),
}
}
}
pub fn to_vec(&self) -> Vec<Vec<u8>> {
Expand All @@ -535,7 +573,15 @@ impl<'s> Seq064K<'s, U256<'s>> {
data: Some(data),
}
} else {
panic!()
// this is an already valid seq should be safe to call the unwraps.
// also this library shouldn't be used for priduction envs so is ok do thigs like this
// one
let data = self.seq.unwrap().parse().unwrap();
let data = data.into_iter().map(|i| i.into_static()).collect();
Seq064K {
seq: None,
data: Some(data),
}
}
}
}
Expand Down

0 comments on commit 9e48228

Please sign in to comment.