Skip to content

Commit 74815bf

Browse files
committed
Change Codec trait to make codecs composable
* Replace `const CODE` with * `fn to_code` * `fn try_from_code`
1 parent a094e09 commit 74815bf

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

src/codec.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@ use std::io::{BufRead, Write};
1010
/// Each IPLD codec implementation should implement this Codec trait. This way codecs can be more
1111
/// easily exchanged or combined.
1212
pub trait Codec<T>: Links {
13-
/// The multicodec code of the IPLD codec.
14-
const CODE: u64;
1513
/// The error that is returned if encoding or decoding fails.
1614
type Error;
1715

16+
/// The multicodec code of the IPLD codec.
17+
fn to_code(&self) -> u64;
18+
/// Attempt to convert from a `u64` code to this `Codec`.
19+
fn try_from_code(code: u64) -> Option<Self> where Self: Sized;
20+
1821
/// Decode a reader into the desired type.
1922
fn decode<R: BufRead>(reader: R) -> Result<T, Self::Error>;
2023
/// Encode a type into a writer.

0 commit comments

Comments
 (0)