Skip to content

Commit 3da17fb

Browse files
committed
Separate example from core
1 parent 6ac7bbe commit 3da17fb

File tree

5 files changed

+54
-39
lines changed

5 files changed

+54
-39
lines changed

Cargo.toml

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
1-
[package]
2-
authors = ["Vladimir Serbinenko <[email protected]>"]
3-
edition = "2018"
4-
readme = "README.md"
5-
name = "grub-rust-hello"
6-
version = "0.1.0"
7-
8-
[dependencies]
1+
[workspace]
2+
members = ["grub_rust_core", "grub_rust_example"]
3+
resolver = "3"
94

105
[profile.release]
116
codegen-units = 1 # better optimizations
@@ -20,5 +15,5 @@ debug = true # symbols are nice and they don't increase the size on Flash
2015
lto = true # better optimizations
2116
panic="abort"
2217

23-
[lib]
24-
crate-type = ["lib", "cdylib"]
18+
[workspace.dependencies]
19+
grub = { path = "grub_rust_core" }

grub_rust_core/Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
authors = ["Vladimir Serbinenko <[email protected]>"]
3+
edition = "2018"
4+
name = "grub"
5+
version = "0.1.0"
6+
7+
[dependencies]
8+
9+
[lib]
10+
crate-type = ["staticlib", "lib"]

src/grub_lib.rs renamed to grub_rust_core/src/lib.rs

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
#![no_std]
2+
#![no_main]
3+
#![feature(extern_types)]
4+
#![feature(rustc_attrs)]
5+
#![feature(format_args_nl)]
6+
#![feature(inherent_str_constructors)]
7+
18
extern crate alloc;
29

310
use alloc::vec::Vec;
@@ -45,7 +52,7 @@ extern "C" {
4552
#[cfg_attr(not(test), rustc_diagnostic_item = "print_macro")]
4653
macro_rules! print {
4754
($($arg:tt)*) => {{
48-
$crate::grub_lib::print_fmt(format_args!($($arg)*));
55+
$crate::print_fmt(format_args!($($arg)*));
4956
}};
5057
}
5158

@@ -56,38 +63,24 @@ macro_rules! println {
5663
$crate::print!("\n")
5764
};
5865
($($arg:tt)*) => {{
59-
$crate::grub_lib::print_fmt(format_args_nl!($($arg)*));
66+
$crate::print_fmt(format_args_nl!($($arg)*));
6067
}};
6168
}
6269

6370
#[macro_export]
6471
macro_rules! dprintln {
6572
($cond:expr, $($args: expr),*) => {
66-
$crate::grub_lib::real_dprintln(file!(), line!(), $cond, format_args_nl!($($args)*));
73+
$crate::real_dprintln(file!(), line!(), $cond, format_args_nl!($($args)*));
6774
}
6875
}
6976

7077
#[macro_export]
7178
macro_rules! eformat {
7279
($num:expr, $($args: expr),*) => {
73-
$crate::grub_lib::GrubError::new_fmt($num, format_args!($($args)*))
74-
}
75-
}
76-
77-
#[macro_export]
78-
macro_rules! format {
79-
($($args: expr),*) => {
80-
$crate::grub_lib::format(format_args!($($args)*))
80+
$crate::GrubError::new_fmt($num, format_args!($($args)*))
8181
}
8282
}
8383

84-
pub fn format(args: Arguments<'_>) -> String {
85-
let mut w = StrWriter {output: "".to_string()};
86-
let _ = fmt::write(&mut w, args);
87-
88-
return w.output;
89-
}
90-
9184
#[no_mangle]
9285
pub extern "C" fn strlen(s: *const c_char) -> usize {
9386
return unsafe { grub_strlen(s) };

grub_rust_example/Cargo.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
authors = ["Vladimir Serbinenko <[email protected]>"]
3+
edition = "2018"
4+
readme = "README.md"
5+
name = "grub-rust-hello"
6+
version = "0.1.0"
7+
8+
[dependencies]
9+
grub = { workspace = true }
10+
11+
[lib]
12+
crate-type = ["lib"]

src/lib.rs renamed to grub_rust_example/src/lib.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,19 @@
55
#![feature(format_args_nl)]
66
#![feature(inherent_str_constructors)]
77

8-
mod grub_lib;
9-
108
extern crate alloc;
9+
extern crate grub;
1110

1211
use alloc::string::ToString;
1312
use core::cmp::min;
1413
use core::convert::TryFrom;
1514

15+
use alloc::format;
16+
17+
use grub::dprintln;
18+
use grub::println;
19+
use grub::eformat;
20+
1621
#[link_section = ".modname"]
1722
#[no_mangle]
1823
pub static GRUB_MODNAME: [u8; 11] = *b"rust_hello\0";
@@ -21,14 +26,14 @@ pub static GRUB_MODNAME: [u8; 11] = *b"rust_hello\0";
2126
pub static GRUB_LICENSE: [u8; 15] = *b"LICENSE=GPLv3+\0";
2227

2328

24-
fn rust_hello (argv: &[&str]) -> Result<(), grub_lib::GrubError> {
29+
fn rust_hello (argv: &[&str]) -> Result<(), grub::GrubError> {
2530
println!("Hello, world argv={argv:?}");
2631
dprintln!("hello", "hello from debug");
2732
return Ok(());
2833
}
2934

30-
fn rust_err (argv: &[&str]) -> Result<(), grub_lib::GrubError> {
31-
return Err(eformat!(grub_lib::ErrT::Io, "hello from error argv={argv:?}"));
35+
fn rust_err (argv: &[&str]) -> Result<(), grub::GrubError> {
36+
return Err(eformat!(grub::ErrT::Io, "hello from error argv={argv:?}"));
3237
}
3338

3439
fn hexdump (start: u64, buf: &[u8])
@@ -88,11 +93,11 @@ fn hexdump (start: u64, buf: &[u8])
8893
}
8994

9095

91-
fn rust_hexdump (args: &[&str]) -> Result<(), grub_lib::GrubError> {
96+
fn rust_hexdump (args: &[&str]) -> Result<(), grub::GrubError> {
9297
let mut length = 256;
9398
let mut skip = 0;
9499

95-
let mut file = grub_lib::File::open(args[0], &grub_lib::FileType::Hexcat)?;
100+
let mut file = grub::File::open(args[0], &grub::FileType::Hexcat)?;
96101

97102
file.seek(skip);
98103

@@ -119,15 +124,15 @@ fn rust_hexdump (args: &[&str]) -> Result<(), grub_lib::GrubError> {
119124

120125
#[no_mangle]
121126
pub extern "C" fn grub_mod_init() {
122-
grub_lib::Command::register("rust_hello", rust_hello,
127+
grub::Command::register("rust_hello", rust_hello,
123128
"Rust hello", "Say hello from Rust.");
124-
grub_lib::Command::register("rust_err", rust_err,
129+
grub::Command::register("rust_err", rust_err,
125130
"Rust error", "Error out from Rust.");
126-
grub_lib::Command::register("rust_hexdump", rust_hexdump,
131+
grub::Command::register("rust_hexdump", rust_hexdump,
127132
"Rust hexdump", "Hexdump a file from Rust.");
128133
}
129134

130135
#[no_mangle]
131136
pub extern "C" fn grub_mod_fini() {
132-
grub_lib::Command::unregister_all();
137+
grub::Command::unregister_all();
133138
}

0 commit comments

Comments
 (0)