Rust中的panic宏

栏目: 编程语言 · Rust · 发布时间: 5年前

内容简介:如果主线程panic,则整个程序都会终止,如果不是主线程panic则只会终止子线程,其他线程不会异常终止.panic宏源码如下:

panic! 会立即终止程序,同时rust中的 OptionResult 出现 NoneErr 时都会触发 panic

如果主线程panic,则整个程序都会终止,如果不是主线程panic则只会终止子线程,其他线程不会异常终止.

panic宏源码如下:

#[macro_export]
#[stable(feature = "rust1", since = "1.0.0")]
#[allow_internal_unstable]
macro_rules! panic {
    () => ({
        panic!("explicit panic")
    });
    ($msg:expr) => ({
        $crate::rt::begin_panic($msg, &(file!(), line!(), __rust_unstable_column!()))
    });
    ($fmt:expr, $($arg:tt)+) => ({
        $crate::rt::begin_panic_fmt(&format_args!($fmt, $($arg)+),
                                    &(file!(), line!(), __rust_unstable_column!()))
    });
}

复制代码

有时我们希望将panic信息输出到日志中,标准库中的std::panic可以满足需求。可以设置一个类似于windows中的钩子函数,在panic发出后,在panic运行时之前,触发钩子函数去处理这个panic信息。panic信息被保存在PanicInfo结构体中。

///Registers a custom panic hook, replacing any that was previously registered.
pub fn set_hook(hook: Box<Fn(&PanicInfo) + Sync + Send + 'static>)
复制代码
The panic hook is invoked when a thread panics, but before the panic runtime is invoked. As such, the hook will run with both the aborting and unwinding runtimes. The default hook prints a message to standard error and generates a backtrace if requested, but this behavior can be customized with the set_hook and take_hook functions.
#[macro_use]
extern crate log;
extern crate simple_logger;
use std::thread;
use std::boxed::Box;
use std::panic;

fn main() {
    simple_logger::init().unwrap();
    panic::set_hook(Box::new(|panic_info|{
        error!("panic info: {:?},panic occurred in {:?}",panic_info.payload().downcast_ref::<&str>(),panic_info.location());
    }));

    thread::spawn(||{
        panic!("child thread panic test!");
    });

    loop{
    }
}
复制代码

downcast_ref的源码

#[stable(feature = "rust1", since = "1.0.0")]
    #[inline]
    pub fn downcast_ref<T: Any>(&self) -> Option<&T> {
        if self.is::<T>() {
            unsafe {
                Some(&*(self as *const Any as *const T))
            }
        } else {
            None
        }
    }
复制代码

但上面的代码如果是 Result 或者 Optionunwrap 触发的 panic ,则输出信息中 payload 项会是 None ,原因可以看上面 downcast_ref 的源码,进入 downcast_ref 后会有个判断条件如果不是类型 T 则就会返回 None 。如果我们想处理 unwrap 触发的 panic ,可以使用下面的示例代码,但是是有条件的,会用到 unsafe 代码段。

#[macro_use]
extern crate log;
extern crate simple_logger;
use std::thread;
use std::boxed::Box;
use std::panic;
use std::any::Any;

#[derive(Debug)]
struct PanicErr{
    info:String,
    value:i32,
}

fn main() {
    simple_logger::init().unwrap();
    panic::set_hook(Box::new(|panic_info|{
        unsafe {
            let s=&*(panic_info.payload() as *const Any as *const &str);
            error!("panic info: {},occurred in {:?}",s,panic_info.location());
        }
    }));

    thread::spawn(||{
        let e:Result<(),bool>=Err(false);
        e.unwrap();
    });

    let a:Result<(),PanicErr>=Err(PanicErr{info:"error info".to_string(),value:10});
    a.unwrap();

    loop{
    }
}

复制代码

参考文档:std::panic


以上所述就是小编给大家介绍的《Rust中的panic宏》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

算法统治世界——智能经济的隐形秩序

算法统治世界——智能经济的隐形秩序

徐恪、李沁 / 清华大学出版社有限公司 / 2017-11-15 / CNY 69.00

今天,互联网已经彻底改变了经济系统的运行方式,经济增长的决定性要素已经从物质资料的增加转变成为信息的增长。但是,只有信息的快速增长是不够的,这些增长的信息还必须是“有序”的。只有“有序”才能使信息具有价值,能够为人所用,能够指导我们实现商业的新路径。这种包含在信息里的隐形秩序才是今天信息世界的真正价值所在。经济系统的运行确实是纷繁复杂的,但因为算法的存在,这一切变得有律可循,算法也成为新经济系统里......一起来看看 《算法统治世界——智能经济的隐形秩序》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具