-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make Codec
s composable
#18
Draft
expede
wants to merge
6
commits into
ipld:main
Choose a base branch
from
expede:compose-codecs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
74815bf
Change `Codec` trait to make codecs composable
expede 5007760
Add `&self` to `fn links` for composable codecs
expede 200feba
Also parameterize encode/decode
expede 4ec9d49
Typo
expede 323e779
Bump deps
expede b56d3af
Trying to align dev-deps for doctests
expede File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,25 +10,28 @@ 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I'd just call this |
||
/// 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>; | ||
fn decode<R: BufRead>(&self, reader: R) -> Result<T, Self::Error>; | ||
/// Encode a type into a writer. | ||
fn encode<W: Write>(writer: W, data: &T) -> Result<(), Self::Error>; | ||
fn encode<W: Write>(&self, writer: W, data: &T) -> Result<(), Self::Error>; | ||
|
||
/// Decode a slice into the desired type. | ||
fn decode_from_slice(bytes: &[u8]) -> Result<T, Self::Error> { | ||
Self::decode(bytes) | ||
fn decode_from_slice(&self, bytes: &[u8]) -> Result<T, Self::Error> { | ||
self.decode(bytes) | ||
} | ||
|
||
/// Encode a type into bytes. | ||
fn encode_to_vec(data: &T) -> Result<Vec<u8>, Self::Error> { | ||
fn encode_to_vec(&self, data: &T) -> Result<Vec<u8>, Self::Error> { | ||
let mut output = Vec::new(); | ||
Self::encode(&mut output, data)?; | ||
self.encode(&mut output, data)?; | ||
Ok(output) | ||
} | ||
} | ||
|
@@ -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>; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm surprised this was necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's probably not; it's having a hard time picking up the codec in the doctests and I'm troubleshooting. Because this spans multiple repos I need to push to git, so I've flipped the PR to
Draft
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Multiple repos with a dependency cycle, even (on the
dev-dependencies
)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, I know that it's not required because in the library that I'm actually working on I now actually need fewer type annotations. For example: