Skip to content
Draft
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I'd just call this code().

/// 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>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume these now need to take &self?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically, this is going to become a "by value" instead of a "by type" trait.

/// Encode a type into a writer.
Expand All @@ -39,5 +42,5 @@ pub trait Links {
type LinksError;

/// Return all links (CIDs) that the given encoded data contains.
fn links(bytes: &[u8]) -> Result<impl Iterator<Item = Cid>, Self::LinksError>;
fn links(&self, bytes: &[u8]) -> Result<impl Iterator<Item = Cid>, Self::LinksError>;
}