@@ -16,18 +16,26 @@ and one for deallocation. A freestanding program that uses the `Box`
16
16
sugar for dynamic allocations via ` malloc ` and ` free ` :
17
17
18
18
``` 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 )]
20
20
#![no_std]
21
21
use core::intrinsics;
22
22
use core::panic::PanicInfo;
23
+ use core::ptr::NonNull;
23
24
24
25
extern crate libc;
25
26
26
- struct Unique<T>(*mut T );
27
+ struct Unique<T>(NonNull<T> );
27
28
28
29
#[lang = "owned_box"]
29
30
pub struct Box<T>(Unique<T>);
30
31
32
+ impl<T> Box<T> {
33
+ pub fn new(x: T) -> Self {
34
+ #[rustc_box]
35
+ Box::new(x)
36
+ }
37
+ }
38
+
31
39
#[lang = "exchange_malloc"]
32
40
unsafe fn allocate(size: usize, _align: usize) -> *mut u8 {
33
41
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) {
47
55
48
56
#[start]
49
57
fn main(_argc: isize, _argv: *const *const u8) -> isize {
50
- let _x = box 1 ;
58
+ let _x = Box::new(1) ;
51
59
52
60
0
53
61
}
54
62
55
63
#[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() }
57
65
#[no_mangle] pub extern fn rust_eh_register_frames () {}
58
66
#[no_mangle] pub extern fn rust_eh_unregister_frames () {}
59
67
```
0 commit comments