From 6d24ee84f407eddbc5506fe52098f0f35411b069 Mon Sep 17 00:00:00 2001 From: pointer-to-bios Date: Wed, 24 Jan 2024 02:31:17 +0800 Subject: [PATCH] =?UTF-8?q?rust=E4=B8=8D=E4=BD=BF=E7=94=A8=E6=A0=87?= =?UTF-8?q?=E5=87=86=E5=BA=93=EF=BC=8C=E5=8F=AA=E4=BD=BF=E7=94=A8core?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rustlib | 2 +- src/Makefile | 12 ++---------- src/kernel/clock/time.rs | 13 +++++++------ src/kernel/klog.rs | 6 +++++- src/kernel/sync/rwlock.rs | 17 ++++++++--------- src/kernel/tty/tty.rs | 27 +++++++++++++++------------ src/lib.rs | 4 +++- 7 files changed, 41 insertions(+), 40 deletions(-) diff --git a/rustlib b/rustlib index 0d9cd08..a7d79f8 160000 --- a/rustlib +++ b/rustlib @@ -1 +1 @@ -Subproject commit 0d9cd0822c355e784c6f4ff5ac2fd88f6d917ea7 +Subproject commit a7d79f8d3d865c93dad38796530e6a4dc53a0377 diff --git a/src/Makefile b/src/Makefile index 9ff80e0..6ea2cf8 100644 --- a/src/Makefile +++ b/src/Makefile @@ -37,16 +37,8 @@ ifeq (${ARCH},x86_64) endif RUSTLIB_PATH = ../rustlib/${ARCH}/lib -RUST_LIBS = "${RUSTLIB_PATH}/libaddr2line.rlib" "${RUSTLIB_PATH}/libadler.rlib" \ - "${RUSTLIB_PATH}/liballoc.rlib" "${RUSTLIB_PATH}/libcfg_if.rlib" "${RUSTLIB_PATH}/libcompiler_builtins.rlib" \ - "${RUSTLIB_PATH}/libcore.rlib" "${RUSTLIB_PATH}/libgetopts.rlib" "${RUSTLIB_PATH}/libgimli.rlib" \ - "${RUSTLIB_PATH}/libhashbrown.rlib" "${RUSTLIB_PATH}/libmemchr.rlib" "${RUSTLIB_PATH}/libminiz_oxide.rlib" \ - "${RUSTLIB_PATH}/libobject.rlib" "${RUSTLIB_PATH}/libpanic_abort.rlib" "${RUSTLIB_PATH}/libpanic_unwind.rlib" \ - "${RUSTLIB_PATH}/libproc_macro.rlib" "${RUSTLIB_PATH}/libprofiler_builtins.rlib" \ - "${RUSTLIB_PATH}/librustc_demangle.rlib" "${RUSTLIB_PATH}/librustc_std_workspace_alloc.rlib" \ - "${RUSTLIB_PATH}/librustc_std_workspace_core.rlib" "${RUSTLIB_PATH}/librustc_std_workspace_std.rlib" \ - "${RUSTLIB_PATH}/libstd_detect.rlib" "${RUSTLIB_PATH}/libstd.rlib" "${RUSTLIB_PATH}/libsysroot.rlib" \ - "${RUSTLIB_PATH}/libtest.rlib" "${RUSTLIB_PATH}/libunicode_width.rlib" "${RUSTLIB_PATH}/libunwind.rlib" +RUST_LIBS = "${RUSTLIB_PATH}/liballoc.rlib" "${RUSTLIB_PATH}/libcompiler_builtins.rlib" \ + "${RUSTLIB_PATH}/libcore.rlib" "${RUSTLIB_PATH}/librustc_std_workspace_core.rlib" ################################ diff --git a/src/kernel/clock/time.rs b/src/kernel/clock/time.rs index b0b0e66..7f56363 100644 --- a/src/kernel/clock/time.rs +++ b/src/kernel/clock/time.rs @@ -1,4 +1,6 @@ -use std::{cmp::Ordering, fmt::Debug, ops::Sub, time::Duration}; +use core::{cmp::Ordering, ops::Sub, time::Duration}; + +use alloc::{format, string::ToString}; extern "C" { fn system_time_get() -> usize; @@ -14,8 +16,8 @@ pub struct SystemTime { ns_time: usize, } -impl Debug for SystemTime { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { +impl ToString for SystemTime { + fn to_string(&self) -> alloc::string::String { let second_dur = 1000usize; let minute_dur = second_dur * 60; let hour_dur = minute_dur * 60; @@ -93,8 +95,7 @@ impl Debug for SystemTime { minute /= minute_dur; let milisec = second % second_dur; second /= second_dur; - write!( - f, + format!( "[{:02}-{:02}-{:02} {:02}:{:02}:{:02}.{:03}]", year, month, day, hour, minute, second, milisec ) @@ -118,7 +119,7 @@ impl Ord for SystemTime { } impl PartialOrd for SystemTime { - fn partial_cmp(&self, other: &Self) -> Option { + fn partial_cmp(&self, other: &Self) -> Option { match self.unix_time.partial_cmp(&other.unix_time) { Some(Ordering::Equal) => self.ns_time.partial_cmp(&other.ns_time), ord => ord, diff --git a/src/kernel/klog.rs b/src/kernel/klog.rs index 41e4f23..3e157fb 100644 --- a/src/kernel/klog.rs +++ b/src/kernel/klog.rs @@ -1,3 +1,7 @@ +use alloc::string::ToString; +use alloc::{format, vec::Vec}; +use alloc::vec; + use super::{ clock::time::SystemTime, tty::tty::{Color, Message, MessageBuilder}, @@ -149,7 +153,7 @@ impl Iterator for LogIterator { if let Some((time, msg)) = self.logs.first() { Some( MessageBuilder::new() - .message(format!("{:?}", time)) + .message(format!("{}", time.to_string())) .append(MessageBuilder::from_message(msg)) .build(), ) diff --git a/src/kernel/sync/rwlock.rs b/src/kernel/sync/rwlock.rs index be7a430..76d2f36 100644 --- a/src/kernel/sync/rwlock.rs +++ b/src/kernel/sync/rwlock.rs @@ -1,19 +1,18 @@ -use std::{ - ops::{Deref, DerefMut}, - ptr::null_mut, -}; +use core::{ops::{Deref, DerefMut}, ptr::null_mut}; + +use alloc::{vec, vec::Vec}; /// ## RwLock /// 读写锁 -/// +/// /// * 对于写入操作: -/// +/// /// 锁没有占用时,获取锁,并执行闭包。 -/// +/// /// 当锁已经被占用,将闭包悬挂。 -/// +/// /// 释放锁时,将所有悬挂的闭包都执行。 -/// +/// /// 正在释放锁时,`write`方法返回`Err(())` /// /// ``` diff --git a/src/kernel/tty/tty.rs b/src/kernel/tty/tty.rs index 9ab4c65..5cb1011 100644 --- a/src/kernel/tty/tty.rs +++ b/src/kernel/tty/tty.rs @@ -1,4 +1,11 @@ -use std::{fmt::Debug, ptr::null_mut}; +use core::ptr::null_mut; + +use alloc::{ + format, + string::{String, ToString}, + vec, + vec::Vec, +}; extern "C" { fn tty_new(tty_type: u8, mode: u8) -> *mut u8; @@ -140,13 +147,7 @@ pub struct Color(pub u8, pub u8, pub u8); impl ToString for Color { fn to_string(&self) -> String { - format!("Color({:02x}, {:02x}, {:02x})", self.0, self.1, self.2) - } -} - -impl Debug for Color { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{:02x}{:02x}{:02x}", self.0, self.1, self.2) + format!("{:02x}{:02x}{:02x}", self.0, self.1, self.2) } } @@ -165,11 +166,13 @@ impl ColorPair { } } -impl Debug for ColorPair { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { +impl ToString for ColorPair { + fn to_string(&self) -> String { match self { - ColorPair::Reset => write!(f, "\x1b{{}}"), - ColorPair::Color { fore, back } => write!(f, "\x1b{{{:?}{:?}}}", fore, back), + ColorPair::Reset => format!("\x1b{{}}"), + ColorPair::Color { fore, back } => { + format!("\x1b{{{}{}}}", fore.to_string(), back.to_string()) + } } } } diff --git a/src/lib.rs b/src/lib.rs index be290dc..9f726b0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,6 @@ -extern crate core; +#![no_std] + +extern crate alloc; pub mod kernel; pub mod libk;