refactor: remove transmute in favor of safe code

Signed-off-by: Alexey Galakhov <agalakhov@snapview.de>
pull/37/head
Alexey Galakhov 7 years ago
parent c0cde68999
commit 977c6e7c7a
  1. 12
      src/protocol/frame/frame.rs

@ -1,11 +1,10 @@
use std::fmt; use std::fmt;
use std::borrow::Cow; use std::borrow::Cow;
use std::mem::transmute;
use std::io::{Cursor, Read, Write, ErrorKind}; use std::io::{Cursor, Read, Write, ErrorKind};
use std::default::Default; use std::default::Default;
use std::string::{String, FromUtf8Error}; use std::string::{String, FromUtf8Error};
use std::result::Result as StdResult; use std::result::Result as StdResult;
use byteorder::{ByteOrder, ReadBytesExt, NetworkEndian}; use byteorder::{ByteOrder, ReadBytesExt, WriteBytesExt, NetworkEndian};
use bytes::BufMut; use bytes::BufMut;
use error::{Error, Result}; use error::{Error, Result};
@ -260,11 +259,10 @@ impl Frame {
#[inline] #[inline]
pub fn close(msg: Option<CloseFrame>) -> Frame { pub fn close(msg: Option<CloseFrame>) -> Frame {
let payload = if let Some(CloseFrame { code, reason }) = msg { let payload = if let Some(CloseFrame { code, reason }) = msg {
let raw: [u8; 2] = unsafe { let mut p = Vec::with_capacity(reason.as_bytes().len() + 2);
let u: u16 = code.into(); p.write_u16::<NetworkEndian>(code.into()).unwrap(); // can't fail
transmute(u.to_be()) p.extend_from_slice(reason.as_bytes());
}; p
[&raw[..], reason.as_bytes()].concat()
} else { } else {
Vec::new() Vec::new()
}; };

Loading…
Cancel
Save