@@ -16,18 +16,26 @@ and one for deallocation. A freestanding program that uses the `Box`
1616sugar for dynamic allocations via ` malloc ` and ` free ` :
1717
1818``` rust,ignore (libc-is-finicky)
19- #![feature(lang_items, box_syntax, start, libc, core_intrinsics, rustc_private)]
19+ #![feature(lang_items, start, libc, core_intrinsics, rustc_private, rustc_attrs )]
2020#![no_std]
2121use core::intrinsics;
2222use core::panic::PanicInfo;
23+ use core::ptr::NonNull;
2324
2425extern crate libc;
2526
26- struct Unique<T>(*mut T );
27+ struct Unique<T>(NonNull<T> );
2728
2829#[lang = "owned_box"]
2930pub struct Box<T>(Unique<T>);
3031
32+ impl<T> Box<T> {
33+ pub fn new(x: T) -> Self {
34+ #[rustc_box]
35+ Box::new(x)
36+ }
37+ }
38+
3139#[lang = "exchange_malloc"]
3240unsafe fn allocate(size: usize, _align: usize) -> *mut u8 {
3341 let p = libc::malloc(size as libc::size_t) as *mut u8;
@@ -47,13 +55,13 @@ unsafe fn box_free<T: ?Sized>(ptr: *mut T) {
4755
4856#[start]
4957fn main(_argc: isize, _argv: *const *const u8) -> isize {
50- let _x = box 1 ;
58+ let _x = Box::new(1) ;
5159
5260 0
5361}
5462
5563#[lang = "eh_personality"] extern fn rust_eh_personality() {}
56- #[lang = "panic_impl"] extern fn rust_begin_panic(info : &PanicInfo) -> ! { unsafe { intrinsics::abort() } }
64+ #[lang = "panic_impl"] extern fn rust_begin_panic(_info : &PanicInfo) -> ! { intrinsics::abort() }
5765#[no_mangle] pub extern fn rust_eh_register_frames () {}
5866#[no_mangle] pub extern fn rust_eh_unregister_frames () {}
5967```
0 commit comments