How can you allocate a raw mutable pointer in stable Rust?

Viewed 4577

I was trying to build a naive implementation of a custom String-like struct with small string optimization. Now that unions are allowed in stable Rust, I came up with the following code:

struct Large {
    capacity: usize,
    buffer: *mut u8,
}

struct Small([u8; 16]);

union Container {
    large: Large,
    small: Small,
}

struct MyString {
    len: usize,
    container: Container,
}

I can't seem to find a way how to allocate that *mut u8. Is it possible to do in stable Rust? It looks like using alloc::heap would work, but it is only available in nightly.

3 Answers
Related