Skip to content

Commit

Permalink
chore: StreamId.to_vec cannot fail
Browse files Browse the repository at this point in the history
  • Loading branch information
stbrody committed May 16, 2024
1 parent d15af99 commit 7efcd7d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 13 deletions.
2 changes: 1 addition & 1 deletion core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ macro_rules! impl_multi_base {
type Error = anyhow::Error;

fn try_from(v: &StreamId) -> Result<Self, Self::Error> {
let v = v.to_vec()?;
let v = v.to_vec();
Ok(Self::from(v))
}
}
Expand Down
18 changes: 8 additions & 10 deletions core/src/stream_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,13 @@ impl StreamId {
}

/// Convert the stream id to a vector
pub fn to_vec(&self) -> anyhow::Result<Vec<u8>> {
pub fn to_vec(&self) -> Vec<u8> {
// Use self.len() here when we have cid@0.10
let buf = Vec::new();
let mut writer = std::io::BufWriter::new(buf);
self.write(&mut writer)?;
Ok(writer.into_inner()?)
// safe to unwrap because self.write should never fail.
self.write(&mut writer).unwrap();
writer.into_inner().unwrap()
}
}

Expand All @@ -122,7 +123,7 @@ impl TryInto<Bytes> for &StreamId {
type Error = anyhow::Error;

fn try_into(self) -> Result<Bytes, Self::Error> {
Ok(Bytes::from(self.to_vec()?))
Ok(Bytes::from(self.to_vec()))
}
}

Expand All @@ -137,12 +138,9 @@ impl std::str::FromStr for StreamId {

impl std::fmt::Display for StreamId {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
if let Ok(b) = self.to_vec() {
let s = multibase::encode(Base::Base36Lower, b);
write!(f, "{}", s)
} else {
Err(std::fmt::Error)
}
let b = self.to_vec();
let s = multibase::encode(Base::Base36Lower, b);
write!(f, "{}", s)
}
}

Expand Down
3 changes: 1 addition & 2 deletions event/src/unvalidated/payload/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ impl Header {
should_index: Option<bool>,
unique: Option<Bytes>,
) -> Self {
// TODO: Builder should properly handle errors from serializing StreamID to Bytes
let model: Option<Bytes> = model.map(|id| Bytes::from(id.to_vec().unwrap()));
let model: Option<Bytes> = model.map(|id| Bytes::from(id.to_vec()));
Self {
controllers,
sep,
Expand Down

0 comments on commit 7efcd7d

Please sign in to comment.