mirror of
https://github.com/rust-lang-nursery/rust-cookbook
synced 2025-02-16 12:18:27 +00:00
Set up pages
This commit is contained in:
parent
28245801d5
commit
02a3497a35
3 changed files with 60 additions and 55 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -1,2 +1,4 @@
|
|||
target
|
||||
*~
|
||||
|
||||
*.swp
|
||||
|
|
56
README.md
56
README.md
|
@ -2,7 +2,7 @@
|
|||
|
||||
A practical guide to the Rust crate ecosystem.
|
||||
|
||||
- [Read and write integers in little-endian byte order](#byteorder)
|
||||
[Read and write integers in little-endian byte order](pages/byteorder.md)
|
||||
[![byteorder][byteorder-badge]][byteorder]
|
||||
|
||||
|
||||
|
@ -52,60 +52,6 @@ makes the `?` operator work
|
|||
For more background on error handling in Rust, read TODO and TODO.
|
||||
|
||||
|
||||
<a id="byteorder"></a>
|
||||
## Read and write integers in little-endian byte order
|
||||
|
||||
[![byteorder][byteorder-badge]][byteorder]
|
||||
|
||||
```rust
|
||||
extern crate byteorder;
|
||||
|
||||
use std::io::Cursor;
|
||||
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
|
||||
|
||||
#[derive(Default, Eq, PartialEq, Debug)]
|
||||
struct Payload {
|
||||
kind: u8,
|
||||
value: u16,
|
||||
}
|
||||
|
||||
fn run() -> Result<()> {
|
||||
let original_payload = Payload::default();
|
||||
let encoded_buf = encode(&original_payload)?;
|
||||
let decoded_payload = decode(&encoded_buf)?;
|
||||
assert_eq!(original_payload, decoded_payload);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn encode(payload: &Payload) -> Result<Vec<u8>> {
|
||||
let mut wtr = vec![];
|
||||
wtr.write_u8(payload.kind)?;
|
||||
wtr.write_u16::<LittleEndian>(payload.value)?;
|
||||
Ok(wtr)
|
||||
}
|
||||
|
||||
fn decode(buf: &[u8]) -> Result<Payload> {
|
||||
let mut rdr = Cursor::new(buf);
|
||||
Ok(Payload {
|
||||
kind: rdr.read_u8()?,
|
||||
value: rdr.read_u16::<LittleEndian>()?,
|
||||
})
|
||||
}
|
||||
|
||||
#[macro_use]
|
||||
extern crate error_chain;
|
||||
mod errors {
|
||||
error_chain! {
|
||||
foreign_links {
|
||||
Io(::std::io::Error);
|
||||
}
|
||||
}
|
||||
}
|
||||
use errors::*;
|
||||
fn main() { run().unwrap() }
|
||||
```
|
||||
|
||||
|
||||
# License
|
||||
|
||||
MIT/Apache-2.0
|
||||
|
|
57
pages/byteorder.md
Normal file
57
pages/byteorder.md
Normal file
|
@ -0,0 +1,57 @@
|
|||
#Byteorder
|
||||
## Read and write integers in little-endian byte order
|
||||
|
||||
[![byteorder][byteorder-badge]][byteorder]
|
||||
|
||||
```rust
|
||||
extern crate byteorder;
|
||||
|
||||
use std::io::Cursor;
|
||||
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
|
||||
|
||||
#[derive(Default, Eq, PartialEq, Debug)]
|
||||
struct Payload {
|
||||
kind: u8,
|
||||
value: u16,
|
||||
}
|
||||
|
||||
fn run() -> Result<()> {
|
||||
let original_payload = Payload::default();
|
||||
let encoded_buf = encode(&original_payload)?;
|
||||
let decoded_payload = decode(&encoded_buf)?;
|
||||
assert_eq!(original_payload, decoded_payload);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn encode(payload: &Payload) -> Result<Vec<u8>> {
|
||||
let mut wtr = vec![];
|
||||
wtr.write_u8(payload.kind)?;
|
||||
wtr.write_u16::<LittleEndian>(payload.value)?;
|
||||
Ok(wtr)
|
||||
}
|
||||
|
||||
fn decode(buf: &[u8]) -> Result<Payload> {
|
||||
let mut rdr = Cursor::new(buf);
|
||||
Ok(Payload {
|
||||
kind: rdr.read_u8()?,
|
||||
value: rdr.read_u16::<LittleEndian>()?,
|
||||
})
|
||||
}
|
||||
|
||||
#[macro_use]
|
||||
extern crate error_chain;
|
||||
mod errors {
|
||||
error_chain! {
|
||||
foreign_links {
|
||||
Io(::std::io::Error);
|
||||
}
|
||||
}
|
||||
}
|
||||
use errors::*;
|
||||
fn main() { run().unwrap() }
|
||||
```
|
||||
|
||||
<!-- Links -->
|
||||
|
||||
[byteorder-badge]: https://img.shields.io/crates/v/rustc-serialize.svg
|
||||
[byteorder]: https://docs.rs/byteorder
|
Loading…
Add table
Reference in a new issue