Skip to content

Commit

Permalink
Change Codec trait to make codecs composable
Browse files Browse the repository at this point in the history
* Replace `const CODE` with
  * `fn to_code`
  * `fn try_from_code`
  • Loading branch information
expede committed Jun 26, 2024
1 parent a094e09 commit 74815bf
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ use std::io::{BufRead, Write};
/// Each IPLD codec implementation should implement this Codec trait. This way codecs can be more
/// easily exchanged or combined.
pub trait Codec<T>: Links {
/// The multicodec code of the IPLD codec.
const CODE: u64;
/// The error that is returned if encoding or decoding fails.
type Error;

/// The multicodec code of the IPLD codec.
fn to_code(&self) -> u64;
/// Attempt to convert from a `u64` code to this `Codec`.
fn try_from_code(code: u64) -> Option<Self> where Self: Sized;

/// Decode a reader into the desired type.
fn decode<R: BufRead>(reader: R) -> Result<T, Self::Error>;
/// Encode a type into a writer.
Expand Down

0 comments on commit 74815bf

Please sign in to comment.