local_encoding/posix.rs
1//! UTF-8 string converting for non-Windows systems.
2use std::io::{Error, ErrorKind, Result};
3use super::Encoder;
4
5/// Convert UTF-8 bytes to String.
6pub struct EncoderUtf8;
7
8impl Encoder for EncoderUtf8 {
9 /// Convert UTF-8 to String.
10 fn to_string(self: &Self, data: &[u8]) -> Result<String> {
11 String::from_utf8(data.to_vec()).map_err(|e| Error::new(ErrorKind::InvalidInput, e))
12 }
13
14 /// Convert String to UTF-8.
15 fn to_bytes(self: &Self, data: &str) -> Result<Vec<u8>> {
16 Ok(data.as_bytes().to_vec())
17 }
18}