Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format output to a byte array with no_std and no allocator?

I want to do something like:

let x = 123;
let mut buf = [0 as u8; 20];
format_to!(x --> buf);
assert_eq!(&buf[..3], &b"123"[..]);

With #![no_std] and without any memory allocator.

As I understand, there is an implementation of core::fmt::Display for u64, and I want to use it if possible.

In other words, I want to do something like format!(...), but without a memory allocator. How can I do this?

like image 448
Andrey Tonkih Avatar asked Sep 14 '16 10:09

Andrey Tonkih


2 Answers

With bare_io:

use bare_io::{Cursor, Write};

let mut buf = [0 as u8; 256];
let mut cur = Cursor::new(&mut buf[..]);
write!(&mut cur, "hello world, stack buf, {}\n\0", 234).expect("!write");
unsafe { puts(buf.as_ptr()) };

With bare_io, smallvec and alloc:

use smallvec::{Array, SmallVec};

struct WriteSmallVec<A: Array<Item = u8>>(SmallVec<A>);
impl<A: Array<Item = u8>> Write for WriteSmallVec<A> {
    fn write(&mut self, buf: &[u8]) -> bare_io::Result<usize> {
        self.0.extend_from_slice(buf);
        Ok(buf.len())
    }
    fn flush(&mut self) -> bare_io::Result<()> {
        Ok(())
    }
}

let mut sv = WriteSmallVec(SmallVec::<[u8; 256]>::new());
write!(&mut sv, "hello world, SmallVec, prev len: {}\n\0", len).expect("!write");
unsafe { puts(sv.0.as_ptr()) };

With bare_io, patched inlinable_string and alloc:

use core::fmt::Write;
use inlinable_string::{InlinableString, StringExt};

let mut is = InlinableString::new();
write!(&mut is, "hello world, InlinableString, {}\n\0", 345).expect("!write");
unsafe { puts(is.as_ptr()) };

Tested in Linux kernel,

cargo build --release -Z build-std=core,alloc --target=x86_64-linux-kernel

enter image description here

Also did some benchmarks, comparing a simple array with SmallVec and InlinableString: https://gitlab.com/artemciy/lin-socks/-/blob/95d2bb96/bench/stack-string.rs

p.s. bare-io has been yanked though.

like image 133
ArtemGr Avatar answered Nov 22 '22 18:11

ArtemGr


Let's start with the standard version:

use std::io::Write;

fn main() {
    let x = 123;
    let mut buf = [0 as u8; 20];
    write!(&mut buf[..], "{}", x).expect("Can't write");
    assert_eq!(&buf[0..3], b"123");
}

If we then remove the standard library:

#![feature(lang_items)]
#![no_std]

use core::panic::PanicInfo;

#[lang = "eh_personality"]
extern "C" fn eh_personality() {}

#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
    loop {}
}

fn main() {
    let x = 123;
    let mut buf = [0 as u8; 20];
    write!(&mut buf[..], "{}", x).expect("Can't write");
    assert_eq!(&buf[0..3], b"123");
}

We get the error

error[E0599]: no method named `write_fmt` found for type `&mut [u8]` in the current scope
  --> src/main.rs:17:5
   |
17 |     write!(&mut buf[..], "{}", x).expect("Can't write");
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

write_fmt is implemented in the core library by core::fmt::Write. If we implement it ourselves, we are able to pass that error:

#![feature(lang_items)]
#![feature(start)]
#![no_std]

use core::panic::PanicInfo;

#[lang = "eh_personality"]
extern "C" fn eh_personality() {}

#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
    loop {}
}

use core::fmt::{self, Write};

struct Wrapper<'a> {
    buf: &'a mut [u8],
    offset: usize,
}

impl<'a> Wrapper<'a> {
    fn new(buf: &'a mut [u8]) -> Self {
        Wrapper {
            buf: buf,
            offset: 0,
        }
    }
}

impl<'a> fmt::Write for Wrapper<'a> {
    fn write_str(&mut self, s: &str) -> fmt::Result {
        let bytes = s.as_bytes();

        // Skip over already-copied data
        let remainder = &mut self.buf[self.offset..];
        // Check if there is space remaining (return error instead of panicking)
        if remainder.len() < bytes.len() { return Err(core::fmt::Error); }
        // Make the two slices the same length
        let remainder = &mut remainder[..bytes.len()];
        // Copy
        remainder.copy_from_slice(bytes);

        // Update offset to avoid overwriting
        self.offset += bytes.len();

        Ok(())
    }
}

#[start]
fn start(_argc: isize, _argv: *const *const u8) -> isize {
    let x = 123;
    let mut buf = [0 as u8; 20];
    write!(Wrapper::new(&mut buf), "{}", x).expect("Can't write");
    assert_eq!(&buf[0..3], b"123");
    0
}

Note that we are duplicating the behavior of io::Cursor into this wrapper. Normally, multiple writes to a &mut [u8] will overwrite each other. This is good for reusing allocation, but not useful when you have consecutive writes of the same data.

Then it's just a matter of writing a macro if you want to.

You should also be able to use a crate like arrayvec, which has written this code for you. This is untested:

#![feature(lang_items)]
#![feature(start)]
#![no_std]

use core::panic::PanicInfo;

#[lang = "eh_personality"]
extern "C" fn eh_personality() {}

#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
    loop {}
}

use arrayvec::ArrayString; // 0.4.10
use core::fmt::Write;

#[start]
fn start(_argc: isize, _argv: *const *const u8) -> isize {
    let x = 123;
    let mut buf = ArrayString::<[u8; 20]>::new();
    write!(&mut buf, "{}", x).expect("Can't write");
    assert_eq!(&buf, "123");
    0
}
like image 43
Shepmaster Avatar answered Nov 22 '22 18:11

Shepmaster