Skip to content

Commit b6680de

Browse files
authored
Merge pull request #6 from Kern--/master
Make protocol work on stable rust and lastest nightly compiler.
2 parents 9333c8f + 4176a3c commit b6680de

File tree

7 files changed

+61
-28
lines changed

7 files changed

+61
-28
lines changed

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ byteorder = "1.0"
1818
flate2 = { version = "0.2", features = ["zlib"], default-features = false }
1919
uuid = { version = "0.5", optional = true }
2020
error-chain = "0.10"
21+
num-traits = "0.1"
2122

README.md

-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88

99
Easy protocol definitions in Rust.
1010

11-
Requires the nightly compiler.
12-
1311
## Example
1412

1513
```rust

src/errors.rs

+50-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,51 @@
1-
use std;
1+
use std::{self, fmt, error};
2+
3+
4+
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
5+
/// Copy of [TryFromIntError](https://doc.rust-lang.org/std/num/struct.TryFromIntError.html)
6+
/// that works in stable rust
7+
pub struct TryFromIntError { }
8+
9+
impl TryFromIntError {
10+
fn description(&self) -> &str {
11+
"out of range integral type conversion attempted"
12+
}
13+
}
14+
15+
impl fmt::Display for TryFromIntError {
16+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
17+
self.description().fmt(fmt)
18+
}
19+
}
20+
21+
impl error::Error for TryFromIntError {
22+
fn description(&self) -> &str {
23+
self.description()
24+
}
25+
}
26+
27+
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
28+
/// Copy of [CharTryFromError](https://doc.rust-lang.org/std/char/struct.CharTryFromError.html)
29+
/// that works in stable rust
30+
pub struct CharTryFromError { }
31+
32+
impl CharTryFromError {
33+
fn description(&self) -> &str {
34+
"converted integer out of range for `char`"
35+
}
36+
}
37+
38+
impl fmt::Display for CharTryFromError {
39+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
40+
self.description().fmt(f)
41+
}
42+
}
43+
44+
impl error::Error for CharTryFromError {
45+
fn description(&self) -> &str {
46+
self.description()
47+
}
48+
}
249

350
error_chain! {
451
types {
@@ -8,8 +55,8 @@ error_chain! {
855
foreign_links {
956
Io(std::io::Error);
1057
FromUtf8(std::string::FromUtf8Error);
11-
TryFromIntError(std::num::TryFromIntError);
12-
CharTryFromError(std::char::CharTryFromError);
58+
TryFromIntError(TryFromIntError);
59+
CharTryFromError(CharTryFromError);
1360

1461
UuidParseError(::uuid::ParseError) #[cfg(feature = "uuid")];
1562
}

src/lib.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
#![feature(try_from)]
2-
31
//! Simple packet-based protocol definitions in Rust.
42
//!
53
//! * The `packet` module deals with defining packets.
64
//! * The `wire` module deals with transmission of packets.
75
86
pub use self::primitives::{Integer, DynArray, String};
97
pub use self::parcel::Parcel;
10-
pub use self::errors::{Error, ErrorKind, ResultExt};
8+
pub use self::errors::{Error, ErrorKind, ResultExt, CharTryFromError, TryFromIntError};
119

1210
// Must go first because it defines common macros.
1311
#[macro_use]
@@ -28,6 +26,7 @@ extern crate error_chain;
2826

2927
#[cfg(feature = "uuid")]
3028
extern crate uuid;
29+
extern crate num_traits;
3130

3231
/// The default byte ordering.
3332
pub type ByteOrder = ::byteorder::BigEndian;

src/primitives/char.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
use {Parcel, Error};
1+
use {Parcel, Error, CharTryFromError};
22

3+
use std::char;
34
use std::io::prelude::*;
45

56
impl Parcel for char
67
{
78
fn read(read: &mut Read) -> Result<Self, Error> {
8-
use std::convert::TryFrom;
9-
109
let bytes = u32::read(read)?;
11-
Ok(char::try_from(bytes)?)
10+
Ok(char::from_u32(bytes).ok_or(CharTryFromError{ })?)
1211
}
1312

1413
fn write(&self, write: &mut Write) -> Result<(), Error> {

src/primitives/numerics.rs

+2-12
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,11 @@
11
use {Parcel, Error, ByteOrder};
22

33
use std::io::prelude::*;
4-
use std::convert::{TryFrom, TryInto};
5-
use std::num::TryFromIntError;
64

5+
use num_traits::{FromPrimitive, ToPrimitive};
76
use byteorder::{ReadBytesExt, WriteBytesExt};
87

9-
pub trait Integer : Parcel + TryFrom<u8, Error=TryFromIntError> + TryFrom<i8, Error=TryFromIntError> +
10-
TryFrom<u16, Error=TryFromIntError> + TryFrom<i16, Error=TryFromIntError> +
11-
TryFrom<u32, Error=TryFromIntError> + TryFrom<i32, Error=TryFromIntError> +
12-
TryFrom<u64, Error=TryFromIntError> + TryFrom<i64, Error=TryFromIntError> +
13-
TryFrom<usize, Error=TryFromIntError> + TryFrom<isize, Error=TryFromIntError> +
14-
TryInto<u8, Error=TryFromIntError> + TryInto<i8, Error=TryFromIntError> +
15-
TryInto<u16, Error=TryFromIntError> + TryInto<i16, Error=TryFromIntError> +
16-
TryInto<u32, Error=TryFromIntError> + TryInto<i32, Error=TryFromIntError> +
17-
TryInto<u64, Error=TryFromIntError> + TryInto<i64, Error=TryFromIntError> +
18-
TryInto<usize, Error=TryFromIntError> + TryInto<isize, Error=TryFromIntError>
8+
pub trait Integer : Parcel + FromPrimitive + ToPrimitive
199
{
2010

2111
}

src/primitives/util.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use {Parcel, Error};
1+
use {Parcel, Error, TryFromIntError};
22
use primitives::Integer;
33

44
use std::io::prelude::*;
@@ -26,7 +26,7 @@ pub fn read_list_ext<S,T>(read: &mut Read)
2626
where S: Integer,
2727
T: Parcel {
2828
let size = S::read(read)?;
29-
let size: usize = size.try_into()?;
29+
let size: usize = size.to_usize().ok_or(TryFromIntError{ })?;
3030
let mut elements = Vec::with_capacity(size);
3131

3232
for _ in 0..size {
@@ -42,7 +42,7 @@ pub fn write_list_ext<'a,S,T,I>(write: &mut Write, elements: I)
4242
T: Parcel+'a,
4343
I: IntoIterator<Item=&'a T> {
4444
let elements: Vec<_> = elements.into_iter().collect();
45-
let length = S::try_from(elements.len())?;
45+
let length = S::from_usize(elements.len()).ok_or(TryFromIntError{ })?;
4646
length.write(write)?;
4747

4848
for element in elements.into_iter() {
@@ -51,4 +51,3 @@ pub fn write_list_ext<'a,S,T,I>(write: &mut Write, elements: I)
5151

5252
Ok(())
5353
}
54-

0 commit comments

Comments
 (0)