Use the managed crate.

This commit is contained in:
whitequark 2017-01-10 11:04:00 +00:00
parent 08ac98e4bc
commit 67430aa589
6 changed files with 9 additions and 89 deletions

View File

@ -12,6 +12,7 @@ license = "0BSD"
[dependencies]
byteorder = { version = "1.0", default-features = false }
managed = { version = "0.1", default-features = false }
log = { version = "0.3", default-features = false, optional = true }
libc = { version = "0.2.18", optional = true }
@ -20,7 +21,7 @@ env_logger = "0.3"
getopts = "0.2"
[features]
use_std = ["libc"]
use_alloc = []
use_std = ["managed/use_std", "libc"]
use_alloc = ["managed/use_alloc"]
use_log = ["log"]
default = ["use_std", "use_log"]

View File

@ -1,4 +1,5 @@
use Managed;
use managed::Managed;
use wire::{EthernetAddress, IpAddress};
/// An Address Resolution Protocol cache.

View File

@ -68,6 +68,7 @@
//! of a packet, it is still logged correctly and in full.
extern crate byteorder;
extern crate managed;
#[cfg(any(test, feature = "use_std"))]
#[macro_use]
extern crate std;
@ -90,15 +91,11 @@ macro_rules! net_trace {
use core::fmt;
mod managed;
pub mod phy;
pub mod wire;
pub mod iface;
pub mod socket;
pub use managed::Managed;
/// The error type for the networking stack.
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum Error {

View File

@ -1,80 +0,0 @@
use core::ops::{Deref, DerefMut};
use core::fmt;
#[cfg(feature = "use_std")]
use std::boxed::Box;
#[cfg(feature = "use_alloc")]
use alloc::boxed::Box;
#[cfg(feature = "use_std")]
use std::vec::Vec;
/// A managed object.
///
/// This enum can be used to represent exclusive access to objects. In Rust, exclusive access
/// to an object is obtained by either owning the object, or owning a mutable pointer
/// to the object; hence, "managed".
///
/// The purpose of this enum is providing good ergonomics with `std` present while making
/// it possible to avoid having a heap at all (which of course means that `std` is not present).
/// To achieve this, the `Managed::Boxed` variant is only available when the "use_std" or
/// "use_alloc" feature is enabled.
///
/// A function that requires a managed object should be generic over an `Into<Managed<'a, T>>`
/// argument; then, it will be possible to pass either a `Box<T>`, `Vec<T>`, or a `&'a mut T`
/// without any conversion at the call site.
pub enum Managed<'a, T: 'a + ?Sized> {
/// Borrowed variant, either a single element or a slice.
Borrowed(&'a mut T),
/// Owned variant, only available with `std` present.
#[cfg(any(feature = "use_std", feature = "use_alloc"))]
Boxed(Box<T>)
}
impl<'a, T: 'a + fmt::Debug + ?Sized> fmt::Debug for Managed<'a, T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Managed::from({:?})", self.deref())
}
}
impl<'a, T: 'a + ?Sized> From<&'a mut T> for Managed<'a, T> {
fn from(value: &'a mut T) -> Self {
Managed::Borrowed(value)
}
}
#[cfg(any(feature = "use_std", feature = "use_alloc"))]
impl<T: ?Sized + 'static> From<Box<T>> for Managed<'static, T> {
fn from(value: Box<T>) -> Self {
Managed::Boxed(value)
}
}
#[cfg(feature = "use_std")]
impl<T: 'static> From<Vec<T>> for Managed<'static, [T]> {
fn from(value: Vec<T>) -> Self {
Managed::Boxed(value.into_boxed_slice())
}
}
impl<'a, T: 'a + ?Sized> Deref for Managed<'a, T> {
type Target = T;
fn deref(&self) -> &Self::Target {
match self {
&Managed::Borrowed(ref value) => value,
#[cfg(any(feature = "use_std", feature = "use_alloc"))]
&Managed::Boxed(ref value) => value
}
}
}
impl<'a, T: 'a + ?Sized> DerefMut for Managed<'a, T> {
fn deref_mut(&mut self) -> &mut Self::Target {
match self {
&mut Managed::Borrowed(ref mut value) => value,
#[cfg(any(feature = "use_std", feature = "use_alloc"))]
&mut Managed::Boxed(ref mut value) => value
}
}
}

View File

@ -1,7 +1,7 @@
use core::fmt;
use managed::Managed;
use Error;
use Managed;
use wire::{IpProtocol, IpAddress, IpEndpoint};
use wire::{TcpSeqNumber, TcpPacket, TcpRepr, TcpControl};
use socket::{Socket, IpRepr, IpPayload};

View File

@ -1,5 +1,6 @@
use managed::Managed;
use Error;
use Managed;
use wire::{IpProtocol, IpEndpoint};
use wire::{UdpPacket, UdpRepr};
use socket::{Socket, IpRepr, IpPayload};