Skip to content

Commit

Permalink
Seal ByteOrder trait against external implementations
Browse files Browse the repository at this point in the history
Fixes #69
Updates #76
  • Loading branch information
SamWhited authored and BurntSushi committed Mar 28, 2017
1 parent e7cb5ab commit 3ec105c
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ fn pack_size(n: u64) -> usize {
}
}

mod private {
/// Sealed stops crates other than byteorder from implementing any traits that use it.
pub trait Sealed{}
impl Sealed for super::LittleEndian {}
impl Sealed for super::BigEndian {}
}

/// ByteOrder describes types that can serialize integers as bytes.
///
/// Note that `Self` does not appear anywhere in this trait's definition!
Expand All @@ -95,6 +102,8 @@ fn pack_size(n: u64) -> usize {
///
/// This crate provides two types that implement `ByteOrder`: `BigEndian`
/// and `LittleEndian`.
/// This trait is sealed and cannot be implemented for callers to avoid
/// breaking backwards compatibility when adding new derived traits.
///
/// # Examples
///
Expand All @@ -118,7 +127,7 @@ fn pack_size(n: u64) -> usize {
/// assert_eq!(-50_000, BigEndian::read_i16(&buf));
/// ```
pub trait ByteOrder
: Clone + Copy + Debug + Default + Eq + Hash + Ord + PartialEq + PartialOrd {
: Clone + Copy + Debug + Default + Eq + Hash + Ord + PartialEq + PartialOrd + private::Sealed {
/// Reads an unsigned 16 bit integer from `buf`.
///
/// # Panics
Expand Down

0 comments on commit 3ec105c

Please sign in to comment.