Giter VIP home page Giter VIP logo

data-streams's Introduction

Data Streams

Data Streams provides stream extension traits for reading and writing data with streams.

Usage

Add data-streams to your dependencies with cargo add data-streams, or manually in your Cargo.toml:

[dependencies]
data-streams = "2.0.0-pre.2"
use data_streams::{DataSource, DataSink, Result};

fn read(source: &mut impl DataSource) -> Result<()> {
	let int: i32 = source.read_i32()?; // or use generic read_int()
	let str: &str = source.read_utf8_to_end(&mut String::default())?;
	let bytes: &[u8] = source.read_bytes(&mut [0; 128])?;
}

fn write(source: &mut impl DataSink) -> Result<()> {
	source.write_i32(12345)?; // or use generic write_int()
	source.write_utf8("something")?;
	source.write_bytes(&[1, 2, 3, 4, 5])?;
}

Feature flags

The utf8 feature enables reading UTF-8 bytes, with high-performance validation from the simdutf8 crate.

no_std Support

This crate supports no_std and environments without alloc. These are toggled by the std and alloc features respectively. no_std allows use in embedded environments, at the expense of lost implementations for types that would be provided by std::io. Disabling alloc removes reading into vectors/strings.

data-streams's People

Contributors

nighteule5 avatar

Watchers

 avatar

data-streams's Issues

Make traits object-safe

Allow source and sink traits to be dyn by hiding generic methods for unsized impls:

trait DataSource {
    fn read_int<T: PrimInt + Pod>(&mut self) -> Result<T> where Self: Sized;

    // ...
}

Fix `read_exact_bytes` error when the target slice is too large to buffer

The method's default implementation:

pub trait DataSource {
    fn read_exact_bytes<'a>(&mut self, buf: &'a mut [u8]) -> Result<&'a [u8]> {
        let len = buf.len();
        self.require(len)?;
        let bytes = self.read_bytes(buf)?;
        assert_eq!(bytes.len(), len);
        Ok(bytes)
    }
}

If the buffer is too small to hold the length of data required, require will fail. A pathway to bypass the buffer if the slice is very large should be added.

Blocked by #11

Add `read/write_ascii`

Methods for working with ascii::Char slices when full UTF-8 isn't needed would be useful. ASCII validation should also be much faster than UTF-8 validation.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.