Skip to content

Instantly share code, notes, and snippets.

@kpp
Created December 11, 2018 10:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kpp/0901ac29b248eb263f5b6d4e83691a23 to your computer and use it in GitHub Desktop.
Save kpp/0901ac29b248eb263f5b6d4e83691a23 to your computer and use it in GitHub Desktop.
Heapless string for comrade qw1
[package]
name = "stack_string"
version = "0.1.0"
authors = ["Roman Proskuryakov <humbug@deeptown.org>"]
edition = "2018"
[dependencies]
libc = { version = "0.2", default-features = false }
heapless = { version = "0.4" }
[profile.release]
panic = "abort"
[profile.dev]
panic = "abort"
#![feature(lang_items, core_intrinsics)]
#![feature(start)]
#![no_std]
#![no_main]
use core::intrinsics;
use core::panic::PanicInfo;
// Pull in the system libc library for what crt0.o likely requires.
extern crate libc;
extern crate heapless;
use heapless::{String, Vec};
use heapless::consts::*;
#[no_mangle]
pub extern fn main() -> i32 {
let mut users: Vec<UserInfo, U10> = Vec::new();
for i in 0..10 {
let user = UserInfo::new(i);
users.push(user)
.expect("Not enough space to push user");
}
register_users(&users);
0
}
fn register_users(_users: &[UserInfo]) {}
#[derive(Debug)]
struct UserInfo {
id: i32,
name: String<U200>,
pass: String<U200>,
}
impl UserInfo {
pub fn new(id: i32) -> UserInfo {
use core::fmt;
let mut name = String::new();
fmt::write(&mut name, format_args!("user{}", id))
.expect("Not enough space to format user name");
UserInfo {
id,
name,
pass: String::from("***"),
}
}
}
// These functions are used by the compiler, but not
// for a bare-bones hello world. These are normally
// provided by libstd.
#[lang = "eh_personality"]
#[no_mangle]
pub extern fn rust_eh_personality() { }
// This function may be needed based on the compilation target.
#[lang = "eh_unwind_resume"]
#[no_mangle]
pub extern fn rust_eh_unwind_resume() { }
#[lang = "panic_impl"]
#[no_mangle]
pub extern fn rust_begin_panic(_info: &PanicInfo) -> ! {
unsafe { intrinsics::abort() }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment