-
Notifications
You must be signed in to change notification settings - Fork 100
Move Input and Output traits to a separate crate codec-io #122
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
Changes from 1 commit
cc74d80
2f3a8ec
ca4de9b
b11181b
1aabfbc
760695e
File filter
Filter by extension
Conversations
Jump to
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.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "codec-io" | ||
description = "Common Input/Output trait definition shared by encoding libraries." | ||
version = "0.1.0" | ||
authors = ["Parity Technologies <admin@parity.io>"] | ||
license = "Apache-2.0" | ||
repository = "https://github.com/paritytech/parity-scale-codec" | ||
categories = ["encoding"] | ||
edition = "2018" | ||
|
||
[features] | ||
default = ["std"] | ||
std = [] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// Copyright 2017, 2018 Parity Technologies | ||
sorpaas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
//! # Input/Output Trait Definition for Encoding Libraries | ||
//! | ||
//! This library defines `Input` and `Output` traits that can be used for | ||
//! encoding libraries to define their own `Encode` and `Decode` traits. | ||
|
||
#![warn(missing_docs)] | ||
|
||
#![cfg_attr(not(feature = "std"), no_std)] | ||
|
||
extern crate alloc; | ||
|
||
/// Trait that allows reading of data into a slice. | ||
pub trait Input { | ||
/// Error type of this input. | ||
type Error; | ||
|
||
/// Read the exact number of bytes required to fill the given buffer. | ||
/// | ||
/// Note that this function is similar to `std::io::Read::read_exact` and not | ||
/// `std::io::Read::read`. | ||
fn read(&mut self, into: &mut [u8]) -> Result<(), Self::Error>; | ||
|
||
/// Read a single byte from the input. | ||
fn read_byte(&mut self) -> Result<u8, Self::Error> { | ||
let mut buf = [0u8]; | ||
self.read(&mut buf[..])?; | ||
Ok(buf[0]) | ||
} | ||
} | ||
|
||
/// Error for slice-based input. Only used in `no_std` environments. | ||
#[cfg(not(feature = "std"))] | ||
#[derive(PartialEq, Eq, Clone)] | ||
pub enum SliceInputError { | ||
/// Not enough data to fill the buffer. | ||
NotEnoughData, | ||
} | ||
|
||
#[cfg(not(feature = "std"))] | ||
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. Why is this disabled on 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. As far as I understand in |
||
impl<'a> Input for &'a [u8] { | ||
type Error = SliceInputError; | ||
|
||
fn read(&mut self, into: &mut [u8]) -> Result<(), SliceInputError> { | ||
if into.len() > self.len() { | ||
return Err(SliceInputError::NotEnoughData); | ||
} | ||
let len = into.len(); | ||
into.copy_from_slice(&self[..len]); | ||
*self = &self[len..]; | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[cfg(feature = "std")] | ||
impl<R: std::io::Read> Input for R { | ||
type Error = std::io::Error; | ||
|
||
fn read(&mut self, into: &mut [u8]) -> Result<(), std::io::Error> { | ||
(self as &mut dyn std::io::Read).read_exact(into)?; | ||
Ok(()) | ||
} | ||
} | ||
|
||
/// Trait that allows writing of data. | ||
pub trait Output: Sized { | ||
/// Write to the output. | ||
fn write(&mut self, bytes: &[u8]); | ||
|
||
/// Write a single byte to the output. | ||
fn push_byte(&mut self, byte: u8) { | ||
self.write(&[byte]); | ||
} | ||
} | ||
|
||
#[cfg(not(feature = "std"))] | ||
impl Output for alloc::vec::Vec<u8> { | ||
fn write(&mut self, bytes: &[u8]) { | ||
self.extend_from_slice(bytes) | ||
} | ||
} | ||
|
||
#[cfg(feature = "std")] | ||
impl<W: std::io::Write> Output for W { | ||
fn write(&mut self, bytes: &[u8]) { | ||
(self as &mut dyn std::io::Write).write_all(bytes).expect("Codec outputs are infallible"); | ||
} | ||
} |
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.
This version is not released, so please revert this.