Browse Source

Add a use_alloc feature.

whitequark 8 years ago
parent
commit
72e3427ff7
4 changed files with 17 additions and 6 deletions
  1. 1 0
      Cargo.toml
  2. 5 0
      README.md
  3. 4 1
      src/lib.rs
  4. 7 5
      src/managed.rs

+ 1 - 0
Cargo.toml

@@ -14,5 +14,6 @@ env_logger = "0.3"
 
 [features]
 use_std = ["libc"]
+use_alloc = []
 use_log = ["log"]
 default = ["use_std", "use_log"]

+ 5 - 0
README.md

@@ -78,6 +78,11 @@ The `use_std` feature enables use of buffers owned by the networking stack throu
 on `std::boxed::Box`. It also enables `smoltcp::phy::RawSocket` and `smoltcp::phy::TapInterface`,
 if the platform supports it.
 
+### Feature `use_alloc`
+
+The `use_std` feature enables use of buffers owned by the networking stack through a dependency
+on `alloc::boxed::Box`. This only works on nightly rustc.
+
 ### Feature `use_log`
 
 The `use_log` feature enables logging of events within the networking stack through

+ 4 - 1
src/lib.rs

@@ -1,4 +1,5 @@
-#![feature(associated_consts, const_fn, step_by, intrinsics, slice_patterns)]
+#![feature(associated_consts, const_fn, step_by)]
+#![cfg_attr(feature = "use_alloc", feature(alloc))]
 #![no_std]
 
 extern crate byteorder;
@@ -8,6 +9,8 @@ extern crate byteorder;
 extern crate std;
 #[cfg(feature = "use_std")]
 extern crate libc;
+#[cfg(feature = "use_alloc")]
+extern crate alloc;
 #[cfg(feature = "use_log")]
 #[macro_use(trace, log)]
 extern crate log;

+ 7 - 5
src/managed.rs

@@ -1,10 +1,12 @@
 use core::ops::{Deref, DerefMut};
-#[cfg(feature = "use_std")]
+#[cfg(any(feature = "use_std", feature = "use_alloc"))]
 use core::borrow::BorrowMut;
 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;
 
@@ -26,7 +28,7 @@ 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(feature = "use_std")]
+    #[cfg(any(feature = "use_std", feature = "use_alloc"))]
     Owned(Box<BorrowMut<T>>)
 }
 
@@ -42,7 +44,7 @@ impl<'a, T: 'a + ?Sized> From<&'a mut T> for Managed<'a, T> {
     }
 }
 
-#[cfg(feature = "use_std")]
+#[cfg(any(feature = "use_std", feature = "use_alloc"))]
 impl<T, U: BorrowMut<T> + 'static> From<Box<U>> for Managed<'static, T> {
     fn from(value: Box<U>) -> Self {
         Managed::Owned(value)
@@ -63,7 +65,7 @@ impl<'a, T: 'a + ?Sized> Deref for Managed<'a, T> {
     fn deref(&self) -> &Self::Target {
         match self {
             &Managed::Borrowed(ref value) => value,
-            #[cfg(feature = "use_std")]
+            #[cfg(any(feature = "use_std", feature = "use_alloc"))]
             &Managed::Owned(ref value) => (**value).borrow()
         }
     }
@@ -73,7 +75,7 @@ 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(feature = "use_std")]
+            #[cfg(any(feature = "use_std", feature = "use_alloc"))]
             &mut Managed::Owned(ref mut value) => (**value).borrow_mut()
         }
     }