use {types, Type, Error, Array};
use std::io::prelude::*;
use std;
impl Type for std::string::String
{
fn read(read: &mut Read) -> Result<Self, Error> {
let bytes = Vec::<u8>::read(read)?;
Ok(std::string::String::from_utf8(bytes)?)
}
fn write(&self, write: &mut Write) -> Result<(), Error> {
let bytes: Vec<u8> = self.bytes().collect();
bytes.write(write)
}
}
#[derive(Clone, Debug)]
pub struct String<S: types::Integer = u32>
{
pub value: std::string::String,
_a: std::marker::PhantomData<S>,
}
impl<S: types::Integer> String<S>
{
pub fn new(s: std::string::String) -> Self {
String {
value: s,
_a: std::marker::PhantomData,
}
}
}
impl<S: types::Integer> Type for String<S>
{
fn read(read: &mut Read) -> Result<Self, Error> {
let bytes = Array::<S, u8>::read(read)?;
Ok(String::new(std::string::String::from_utf8(bytes.elements)?))
}
fn write(&self, write: &mut Write) -> Result<(), Error> {
let array: Array<S, u8> = Array::new(self.value.bytes().collect());
array.write(write)
}
}