From aedcb8749ee10b791112b469936729126af8a620 Mon Sep 17 00:00:00 2001 From: pointer-to-bios Date: Mon, 19 Feb 2024 01:43:12 +0800 Subject: [PATCH] =?UTF-8?q?Message=E7=B1=BB=E5=9E=8B=E5=A2=9E=E5=8A=A0to?= =?UTF-8?q?=5Fstring=E6=96=B9=E6=B3=95=E7=94=A8=E4=BA=8E=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E5=8C=96=E5=AD=97=E7=AC=A6=E4=B8=B2=EF=BC=8C=E5=B9=B6=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E8=B4=A1=E7=8C=AE=E6=96=87=E6=A1=A3=E7=9A=84=E4=B8=80?= =?UTF-8?q?=E4=BA=9B=E8=AF=B4=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/contribution.md | 7 +++++++ src/kernel/klog.rs | 3 +-- src/kernel/tty/tty.rs | 14 ++++++++++++-- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/docs/contribution.md b/docs/contribution.md index 622a26b..e66325d 100644 --- a/docs/contribution.md +++ b/docs/contribution.md @@ -11,3 +11,10 @@ * 等待审核代码 若你的代码通过审核,将会把你的PR合并到主分支中。 + +## 需要注意的还未解决的问题 + +* rust中所有有关字符串格式化的宏中,出现超过一个不止是`{}`的格式化占位符时内核必然崩溃。 +* rust中所有有关字符串格式化的宏中,出现需要调用自定义的`Debug trait`的类型时内核必然崩溃,推荐定义`ToString trait`并调用`.to_string()`后传入宏参数。 +* 鉴于以上两条原因,不建议在复杂的字符串格式化任务中使用`format!()`宏。推荐通过使用`::kernel::tty::tty::MessageBuilder`构造`kernel::tty::tty::Message`对象,或使用 + `message`宏,并调用此对象的`.to_string()`方法实现格式化字符串。 diff --git a/src/kernel/klog.rs b/src/kernel/klog.rs index 756d84d..628c0ee 100644 --- a/src/kernel/klog.rs +++ b/src/kernel/klog.rs @@ -1,4 +1,3 @@ -use alloc::string::ToString; use alloc::vec; use alloc::vec::Vec; @@ -181,7 +180,7 @@ impl<'a> Iterator for LogIterator<'a> { let res = if let Some((time, msg)) = self.logs.first() { Some( MessageBuilder::new() - .message(&time.to_string()) + .message(time) .append(MessageBuilder::from_message(msg.clone())) .build(), ) diff --git a/src/kernel/tty/tty.rs b/src/kernel/tty/tty.rs index 50b1470..aa3027a 100644 --- a/src/kernel/tty/tty.rs +++ b/src/kernel/tty/tty.rs @@ -181,6 +181,16 @@ pub struct MessageSection { #[derive(Clone)] pub struct Message(Vec); +impl ToString for Message { + fn to_string(&self) -> String { + let mut res = String::new(); + for MessageSection { msg, .. } in self.0.iter() { + res += msg; + } + res + } +} + /// ## MessageBuilder /// /// 使用链式调用模式构造一个消息. @@ -226,7 +236,7 @@ impl MessageBuilder { Self { msg } } - pub fn message(mut self, msg: &str) -> Self { + pub fn message(mut self, msg: &T) -> Self { self.msg.0.push(MessageSection { msg: msg.to_string(), fgcolor: Color(0xee, 0xee, 0xee), @@ -235,7 +245,7 @@ impl MessageBuilder { self } - pub fn message_mut(&mut self, msg: &str) { + pub fn message_mut(&mut self, msg: &T) { self.msg.0.push(MessageSection { msg: msg.to_string(), fgcolor: Color(0xee, 0xee, 0xee),