websocket_base/ws/util/
mod.rs

1//! Utility functions for various portions of Rust-WebSocket.
2
3pub mod header;
4pub mod mask;
5
6use std::str::from_utf8;
7use std::str::Utf8Error;
8
9#[cfg(feature = "async")]
10use tokio_codec::{Framed, FramedParts};
11
12/// Transforms a u8 slice into an owned String
13pub fn bytes_to_string(data: &[u8]) -> Result<String, Utf8Error> {
14	let utf8 = from_utf8(data)?;
15	Ok(utf8.to_string())
16}
17
18/// Updates codec of Framed
19#[cfg(feature = "async")]
20pub fn update_framed_codec<S, B, A>(framed: Framed<S, B>, codec: A) -> Framed<S, A> {
21	let old_parts = framed.into_parts();
22	let mut new_parts = FramedParts::new(old_parts.io, codec);
23	new_parts.read_buf = old_parts.read_buf;
24	new_parts.write_buf = old_parts.write_buf;
25	Framed::from_parts(new_parts)
26}