Giter Site home page Giter Site logo

copstr's Introduction

CI coveralls crates.io doc.rs

copstr: COPy STRing module using const generic for capacity

copstr::Str wraps a fixed-size array of u8 and provides a string-like interface on top. The size is specified using a const generic argument.

The internal u8 array corresponds to UTF-8 encoded chars. All functions guarantee that the contents are valid UTF-8 and return an error if they are not. Truncation only happens at UTF-8 boundaries.

copstr is very useful when we want to add a string-like field to a struct that implements Copy but we don't want to give up this trait.

Example usage

use copstr;
use std::convert::TryFrom;

// Create an owned fixed-size string with size 6 *on the stack*:
let mut string = copstr::Str::<6>::try_from("string")?;

// Use it as a regular string:
println!("contents: {}", string);

// Replace the contents with another string that fits the size 6:
string.replace("str")?;

// Append a letter:
string.push('i')?;

// Instead of returning a potential error, we can instead use
// truncating methods:
string.replace_trunc("stringification");
assert_eq!(string.as_str(), "string");

// `copstr::Str` implements Deref<Target=str>, so all `str`
// methods are available:
let split = format!("{:?}", string.split_at(3));
assert_eq!(split, r#"("str", "ing")"#);

// We can add a `copstr` to a struct without having to give up the
// `Copy` trait:
#[derive(Clone, Copy)]
pub struct Mystruct {
    // ...
    comment: copstr::Str<10>,
}

// We can (and should) create a type alias:
type MyStr = copstr::Str::<4>;

// We can create `copstr` in const contexts:
const TEST: MyStr = MyStr::new_const("TEST");

When using a const context, strings that don't fit generate a compilation error. For instance, the following doesn't compile:

const TEST_BAD: copstr::Str<3> = copstr::Str::<3>::new_const("TEST");

copstr's People

Stargazers

 avatar

Watchers

 avatar  avatar

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.