mostly typed

Cannot Declare Drop Types Statically

So far in Tock we have been allocating the chip and platform definitions statically by declaring them in static mutable Option variables. They are declared as None but on platform initialization, we replace their values with real values:

static mut CHIP : Option<sam4l::Sam4l> = None;
static mut FIRESTORM : Option<Firestorm> = None;

pub unsafe fn init() -> &'static Firestorm {
    CHIP = Some(sam4l::Sam4l::new());
    FIRESTORM = Some(Firestorm {

This works fine as long as the data structures representing chip and platform adhere to the restrictions imposted on statically declared variables. Specifically, Rust forbids statics from containing types with destructors (e.g. types that implement the Drop trait). This is motivated by the desire to avoid executing code before/after main() while not leaking destructors. There is some open discussion to relax this constraint, but it seems unlikely to change soon.

Unfortunately, Tock platforms will contain drivers (obviously), which in turn will need to have references to things with destructors --- specifically memory allocated dynamically from application memory, which needs to be freed when it goes out of scope. For example, the Console driver looks like this:

struct Console<U: UART + 'static> {
    read_buffer: Option<AppPtr<[u8; 40]>>,

AppPtr is a pointer to memory allocated by the driver within the application's memory region (and hidden from the application). It acts like Rust's standard library's Box type in that the underlying memory is implicitly freed when the value goes out of scope, using the Drop trait.

What this means is that we cannot declare the platform variable statically as we had been as it now contains a type with desctructors. The concern for Rust in general may be valid, but we don't care about leaking destructors. The working solution for now is to declare buffers of the approriate size instead of the types themselves and allocate unsafely into those buffers:

static mut FIRESTORM_BUF : [u8; 152] = [0; 152];
let _ : Firestorm = mem::transmute(FIRESTORM_BUF);
let firestorm : &'static mut Firestorm =
    mem::transmute(&mut FIRESTORM_BUF);
*firestorm = Firestorm { ... };

There are two let bindins in the code above. The first is used to ensure that the buffer is sized appropriately (transmute will throw a compile time error if the source and destination types are not of the same size), and the second is casts the buffer to a reference, which is what we actually use.

This ends up being annoying since any changes to the type require a manual change to the size of the buffer. However, const fns how now landed in Rust nightly, and we should expect size_of to be declared a const fn soon, which will allow us to declare buffers like this:

static mut FIRESTORM_BUF : [u8; size_of<Firestorm>()] =
    [0; size_of<Firestorm>()];
4 comment(s)
  1. says:

    Dear friend The post is so helpful,it give me much help,thanks!

  2. Alisa says:

    I was just looking at your mostly typed - Cannot Declare Drop Types Statically website and see that your site has the potential to get a lot of visitors. I just want to tell you, In case you didn't already know... There is a website network which already has more than 16 million users, and most of the users are interested in topics like yours. By getting your website on this network you have a chance to get your site more popular than you can imagine. It is free to sign up and you can read more about it here: - Now, let me ask you... Do you need your site to be successful to maintain your way of life? Do you need targeted traffic who are interested in the services and products you offer? Are looking for exposure, to increase sales, and to quickly develop awareness for your website? If your answer is YES, you can achieve these things only if you get your site on the service I am describing. This traffic service advertises you to thousands, while also giving you a chance to test the service before paying anything. All the popular sites are using this network to boost their traffic and ad revenue! Why aren’t you? And what is better than traffic? It’s recurring traffic! That's how running a successful site works... Here's to your success! Find out more here: - Unsubscribe here:

  3. Alisa says:

    Hi my name is Alisa and I just wanted to send you a quick message here instead of calling you. I discovered your mostly typed - Cannot Declare Drop Types Statically website and noticed you could have a lot more hits. I have found that the key to running a popular website is making sure the visitors you are getting are interested in your subject matter. There is a company that you can get keyword targeted traffic from and they let you try the service for free for 7 days. I managed to get over 300 targeted visitors to day to my website. - Unsubscribe here:

  4. Jennifer says:

    I decided to leave a message here on your mostly typed - Cannot Declare Drop Types Statically page instead of calling you. Do you need more likes for your Facebook Fan Page? The more people that LIKE your website and fanpage on Facebook, the more credibility you will have with new visitors. It works the same for Twitter, Instagram and Youtube. When people visit your page and see that you have a lot of followers, they now want to follow you too. They too want to know what all the hype is and why all those people are following you. Get some free likes, followers, and views just for trying this service I found:

Leave a comment...