Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit373fb60

Browse files
committed
Impl Display for PanicPayload to simplify things.
1 parent224d45c commit373fb60

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

‎core/src/panic.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ pub macro unreachable_2021 {
144144
/// use.
145145
#[unstable(feature ="std_internals", issue ="none")]
146146
#[doc(hidden)]
147-
pubunsafetraitPanicPayload{
147+
pubunsafetraitPanicPayload:crate::fmt::Display{
148148
/// Take full ownership of the contents.
149149
/// The return type is actually `Box<dyn Any + Send>`, but we cannot use `Box` in core.
150150
///

‎std/src/panicking.rs‎

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,12 @@ pub fn begin_panic_handler(info: &core::panic::PanicInfo<'_>) -> ! {
625625
}
626626
}
627627

628+
impl fmt::DisplayforFormatStringPayload<'_>{
629+
fnfmt(&self,f:&mut fmt::Formatter<'_>) -> fmt::Result{
630+
ifletSome(s) =&self.string{ f.write_str(s)}else{ f.write_fmt(*self.inner)}
631+
}
632+
}
633+
628634
structStaticStrPayload(&'staticstr);
629635

630636
unsafeimplPanicPayloadforStaticStrPayload{
@@ -637,21 +643,25 @@ pub fn begin_panic_handler(info: &core::panic::PanicInfo<'_>) -> ! {
637643
}
638644
}
639645

646+
impl fmt::DisplayforStaticStrPayload{
647+
fnfmt(&self,f:&mut fmt::Formatter<'_>) -> fmt::Result{
648+
f.write_str(self.0)
649+
}
650+
}
651+
640652
let loc = info.location().unwrap();// The current implementation always returns Some
641653
let msg = info.message();
642654
crate::sys_common::backtrace::__rust_end_short_backtrace(move ||{
643655
ifletSome(s) = msg.as_str(){
644656
rust_panic_with_hook(
645657
&mutStaticStrPayload(s),
646-
Some(msg),
647658
loc,
648659
info.can_unwind(),
649660
info.force_no_backtrace(),
650661
);
651662
}else{
652663
rust_panic_with_hook(
653664
&mutFormatStringPayload{inner:&msg,string:None},
654-
Some(msg),
655665
loc,
656666
info.can_unwind(),
657667
info.force_no_backtrace(),
@@ -681,7 +691,6 @@ pub const fn begin_panic<M: Any + Send>(msg: M) -> ! {
681691
returncrate::sys_common::backtrace::__rust_end_short_backtrace(move ||{
682692
rust_panic_with_hook(
683693
&mutPayload::new(msg),
684-
None,
685694
loc,
686695
/* can_unwind */true,
687696
/* force_no_backtrace */false,
@@ -719,6 +728,15 @@ pub const fn begin_panic<M: Any + Send>(msg: M) -> ! {
719728
}
720729
}
721730
}
731+
732+
impl<A:Send +'static> fmt::DisplayforPayload<A>{
733+
fnfmt(&self,f:&mut fmt::Formatter<'_>) -> fmt::Result{
734+
match&self.inner{
735+
Some(a) => f.write_str(payload_as_str(a)),
736+
None => process::abort(),
737+
}
738+
}
739+
}
722740
}
723741

724742
fnpayload_as_str(payload:&dynAny) ->&str{
@@ -738,7 +756,6 @@ fn payload_as_str(payload: &dyn Any) -> &str {
738756
/// abort or unwind.
739757
fnrust_panic_with_hook(
740758
payload:&mutdynPanicPayload,
741-
message:Option<fmt::Arguments<'_>>,
742759
location:&Location<'_>,
743760
can_unwind:bool,
744761
force_no_backtrace:bool,
@@ -765,11 +782,7 @@ fn rust_panic_with_hook(
765782
panic_count::MustAbort::AlwaysAbort =>{
766783
// Unfortunately, this does not print a backtrace, because creating
767784
// a `Backtrace` will allocate, which we must avoid here.
768-
ifletSome(message) = message{
769-
rtprintpanic!("aborting due to panic at {location}:\n{message}\n");
770-
}else{
771-
rtprintpanic!("aborting due to panic at {location}\n");
772-
}
785+
rtprintpanic!("aborting due to panic at {location}:\n{payload}\n");
773786
}
774787
}
775788
crate::sys::abort_internal();
@@ -825,6 +838,12 @@ pub fn rust_panic_without_hook(payload: Box<dyn Any + Send>) -> ! {
825838
}
826839
}
827840

841+
impl fmt::DisplayforRewrapBox{
842+
fnfmt(&self,f:&mut fmt::Formatter<'_>) -> fmt::Result{
843+
f.write_str(payload_as_str(&self.0))
844+
}
845+
}
846+
828847
rust_panic(&mutRewrapBox(payload))
829848
}
830849

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp