binstring
A Rust library for storing binary data as a string.
Description
binstring provides a BinString type that wraps a String and provides safe conversion methods between binary data and strings. It's particularly useful when you need to store binary data in a string format while maintaining the ability to access it as both a string and bytes.
Features
- Safe conversion between strings and binary data
- Zero-copy access to underlying data
- Implements common Rust traits (
Clone,Debug,Eq,PartialEq,Hash, etc.) - Provides both owned and borrowed access to data
- Comprehensive documentation with examples
Installation
Add this to your Cargo.toml:
[]
= "0.1"
Usage
Basic Usage
use BinString;
// Create from a string
let bin_str = new;
assert_eq!;
// Create from bytes
let bytes = vec!; // "hello" in bytes
let bin_str = from_bytes;
assert_eq!;
Converting Between Types
use BinString;
// From String
let s = Stringfrom;
let bin_str = from;
// From &str
let bin_str = from;
// From Vec<u8>
let bytes = vec!;
let bin_str = from;
// From &[u8]
let bytes = &;
let bin_str = from;
Accessing Data
use BinString;
let bin_str = new;
// As string slice
assert_eq!;
// As byte slice
assert_eq!;
// Get length
assert_eq!;
// Check if empty
assert!;
Converting Back
use BinString;
let bin_str = new;
// Get the underlying String
let s = bin_str.unwrap;
assert_eq!;
// Or use From trait
let s: String = bin_str.into;
assert_eq!;
Safety
While storing invalid UTF-8 in a String is perfectly safe, attempting to treat the string as valid UTF-8 (for example, by displaying it or using string operations that assume valid UTF-8) may lead to undefined behavior.