An interface for logically owning objects, whether or not heap allocation is available

13 releases (7 breaking)

0.7.1 Jul 11, 2018
0.6.1 Mar 24, 2018
0.4.0 Sep 22, 2017
0.3.0 Mar 5, 2017
0.0.0 Dec 17, 2016

#4 in Embedded development

Download history 45/week @ 2018-05-05 510/week @ 2018-05-12 593/week @ 2018-05-19 165/week @ 2018-05-26 223/week @ 2018-06-02 335/week @ 2018-06-09 334/week @ 2018-06-16 507/week @ 2018-06-23 490/week @ 2018-06-30 77/week @ 2018-07-07 357/week @ 2018-07-14 115/week @ 2018-07-21 242/week @ 2018-07-28

1,306 downloads per month


managed is a library that provides a way to logically own objects, whether or not heap allocation is available. It works with rustc 1.26 or later.


The managed library exists at the intersection of three concepts: heap-less environments, collections and generic code. Consider this struct representing a network interface:

pub struct Interface<'a, 'b: 'a,
    DeviceT:        Device,
    ProtocolAddrsT: BorrowMut<[IpAddress]>,
    SocketsT:       BorrowMut<[Socket<'a, 'b>]>
> {
    device:         DeviceT,
    hardware_addr:  EthernetAddress,
    protocol_addrs: ProtocolAddrsT,
    sockets:        SocketsT,
    phantom:        PhantomData<Socket<'a, 'b>>

There are three things the struct Interface is parameterized over:

  • an object implementing the trait DeviceT, which it owns;
  • a slice of IPAddresses, which it either owns or borrows mutably;
  • a slice of Sockets, which it either owns or borrows mutably, and which further either own or borrow some memory.

The motivation for using BorrowMut is that in environments with heap, the struct ought to own a Vec; on the other hand, without heap there is neither Vec nor Box, and it is only possible to use a &mut. Both of these implement BorrowMut.

Note that owning a BorrowMut in this way does not hide the concrete type inside BorrowMut; if the slice is backed by a Vec then the Vec may still be resized by external code, although not the implementation of Interface.

In isolation, this struct is easy to use. However, when combined with another codebase, perhaps embedded in a scheduler, problems arise. The type parameters have to go somewhere! There are two choices:

  • either the type parameters, whole lot of them, infect the scheduler and push ownership even higher in the call stack (self-mutably-borrowing structs are not usable in safe Rust, so the scheduler could not easily own the slices);
  • or the interface is owned as a boxed trait object, excluding heap-less systems.

Clearly, both options are unsatisfying. Enter managed!


To use the managed library in your project, add the following to Cargo.toml:

managed = "0.6"

The default configuration assumes a hosted environment, for ease of evaluation. You probably want to disable default features and configure them one by one:

managed = { version = "...", default-features = false, features = ["..."] }

Feature std

The std feature enables use of Box, Vec, and BTreeMap through a dependency on the std crate.

Feature alloc

The alloc feature enables use of Box, Vec, and BTreeMap through a dependency on the alloc crate. It requires the use of nightly rustc.

Feature map

The map feature, disabled by default, enables the ManagedMap enum. Its interface is not stable yet and is subject to change. It also requires the use of rustc 1.28 or later.


managed is an interoperability crate: it does not include complex functionality but rather defines an interface that may be used by many downstream crates. It includes three enums:

pub enum Managed<'a, T: 'a + ?Sized> {
    Borrowed(&'a mut T),
    #[cfg(/* Box available */)]

pub enum ManagedSlice<'a, T: 'a> {
    Borrow(&'a mut [T]),
    #[cfg(/* Vec available */)]

// The implementation of ManagedMap is not yet stable, beware!
pub enum ManagedMap<'a, K: Hash + 'a, V: 'a> {
    Borrowed(&'a mut [Option<(K, V)>]),
    #[cfg(/* BTreeMap available */)]
    Owned(BTreeMap<K, V>)

The Managed and ManagedSlice enums have the From implementations from the corresponding types, and Deref/DerefMut implementations to the type T, as well as other helper methods, and ManagedMap is implemented using either a B-tree map or a sorted slice of key-value pairs.

See the full documentation for details.


managed is distributed under the terms of 0-clause BSD license.

See LICENSE-0BSD for details.

0BSD license
  • No runtime deps

Reverse deps