Skip to content

Commit

Permalink
Add 'Panics' headers. Fixes #72
Browse files Browse the repository at this point in the history
  • Loading branch information
brson authored and BurntSushi committed Mar 28, 2017
1 parent 6eb15de commit 0856113
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,48 +120,66 @@ pub trait ByteOrder
: Clone + Copy + Debug + Default + Eq + Hash + Ord + PartialEq + PartialOrd {
/// Reads an unsigned 16 bit integer from `buf`.
///
/// # Panics
///
/// Panics when `buf.len() < 2`.
fn read_u16(buf: &[u8]) -> u16;

/// Reads an unsigned 32 bit integer from `buf`.
///
/// # Panics
///
/// Panics when `buf.len() < 4`.
fn read_u32(buf: &[u8]) -> u32;

/// Reads an unsigned 64 bit integer from `buf`.
///
/// # Panics
///
/// Panics when `buf.len() < 8`.
fn read_u64(buf: &[u8]) -> u64;

/// Reads an unsigned n-bytes integer from `buf`.
///
/// # Panics
///
/// Panics when `nbytes < 1` or `nbytes > 8` or
/// `buf.len() < nbytes`
fn read_uint(buf: &[u8], nbytes: usize) -> u64;

/// Writes an unsigned 16 bit integer `n` to `buf`.
///
/// # Panics
///
/// Panics when `buf.len() < 2`.
fn write_u16(buf: &mut [u8], n: u16);

/// Writes an unsigned 32 bit integer `n` to `buf`.
///
/// # Panics
///
/// Panics when `buf.len() < 4`.
fn write_u32(buf: &mut [u8], n: u32);

/// Writes an unsigned 64 bit integer `n` to `buf`.
///
/// # Panics
///
/// Panics when `buf.len() < 8`.
fn write_u64(buf: &mut [u8], n: u64);

/// Writes an unsigned integer `n` to `buf` using only `nbytes`.
///
/// # Panics
///
/// If `n` is not representable in `nbytes`, or if `nbytes` is `> 8`, then
/// this method panics.
fn write_uint(buf: &mut [u8], n: u64, nbytes: usize);

/// Reads a signed 16 bit integer from `buf`.
///
/// # Panics
///
/// Panics when `buf.len() < 2`.
#[inline]
fn read_i16(buf: &[u8]) -> i16 {
Expand All @@ -170,6 +188,8 @@ pub trait ByteOrder

/// Reads a signed 32 bit integer from `buf`.
///
/// # Panics
///
/// Panics when `buf.len() < 4`.
#[inline]
fn read_i32(buf: &[u8]) -> i32 {
Expand All @@ -178,6 +198,8 @@ pub trait ByteOrder

/// Reads a signed 64 bit integer from `buf`.
///
/// # Panics
///
/// Panics when `buf.len() < 8`.
#[inline]
fn read_i64(buf: &[u8]) -> i64 {
Expand All @@ -186,6 +208,8 @@ pub trait ByteOrder

/// Reads a signed n-bytes integer from `buf`.
///
/// # Panics
///
/// Panics when `nbytes < 1` or `nbytes > 8` or
/// `buf.len() < nbytes`
#[inline]
Expand All @@ -195,6 +219,8 @@ pub trait ByteOrder

/// Reads a IEEE754 single-precision (4 bytes) floating point number.
///
/// # Panics
///
/// Panics when `buf.len() < 4`.
#[inline]
fn read_f32(buf: &[u8]) -> f32 {
Expand All @@ -203,6 +229,8 @@ pub trait ByteOrder

/// Reads a IEEE754 double-precision (8 bytes) floating point number.
///
/// # Panics
///
/// Panics when `buf.len() < 8`.
#[inline]
fn read_f64(buf: &[u8]) -> f64 {
Expand All @@ -211,6 +239,8 @@ pub trait ByteOrder

/// Writes a signed 16 bit integer `n` to `buf`.
///
/// # Panics
///
/// Panics when `buf.len() < 2`.
#[inline]
fn write_i16(buf: &mut [u8], n: i16) {
Expand All @@ -219,6 +249,8 @@ pub trait ByteOrder

/// Writes a signed 32 bit integer `n` to `buf`.
///
/// # Panics
///
/// Panics when `buf.len() < 4`.
#[inline]
fn write_i32(buf: &mut [u8], n: i32) {
Expand All @@ -227,6 +259,8 @@ pub trait ByteOrder

/// Writes a signed 64 bit integer `n` to `buf`.
///
/// # Panics
///
/// Panics when `buf.len() < 8`.
#[inline]
fn write_i64(buf: &mut [u8], n: i64) {
Expand All @@ -235,6 +269,8 @@ pub trait ByteOrder

/// Writes a signed integer `n` to `buf` using only `nbytes`.
///
/// # Panics
///
/// If `n` is not representable in `nbytes`, or if `nbytes` is `> 8`, then
/// this method panics.
#[inline]
Expand All @@ -244,6 +280,8 @@ pub trait ByteOrder

/// Writes a IEEE754 single-precision (4 bytes) floating point number.
///
/// # Panics
///
/// Panics when `buf.len() < 4`.
#[inline]
fn write_f32(buf: &mut [u8], n: f32) {
Expand All @@ -252,6 +290,8 @@ pub trait ByteOrder

/// Writes a IEEE754 double-precision (8 bytes) floating point number.
///
/// # Panics
///
/// Panics when `buf.len() < 8`.
#[inline]
fn write_f64(buf: &mut [u8], n: f64) {
Expand Down
4 changes: 4 additions & 0 deletions src/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ pub trait WriteBytesExt: io::Write {

/// Writes an unsigned n-bytes integer to the underlying writer.
///
/// # Panics
///
/// If the given integer is not representable in the given number of bytes,
/// this method panics. If `nbytes > 8`, this method panics.
#[inline]
Expand All @@ -232,6 +234,8 @@ pub trait WriteBytesExt: io::Write {

/// Writes a signed n-bytes integer to the underlying writer.
///
/// # Panics
///
/// If the given integer is not representable in the given number of bytes,
/// this method panics. If `nbytes > 8`, this method panics.
#[inline]
Expand Down

0 comments on commit 0856113

Please sign in to comment.