注册功能以在Rust程序意外退出期间运行的最佳方法是什么? [英] What's the best way to register a function to run during an unexpected exit of a Rust program?

查看:123
本文介绍了注册功能以在Rust程序意外退出期间运行的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Rust中创建一个终端文本编辑器.编辑器将终端置于原始模式,禁用字符回显等功能,然后在退出时恢复原始终端功能.

I'm creating a terminal text editor in Rust. The editor puts the terminal into raw mode, disabling character echoing and the like, and then restores the original terminal function upon exit.

但是,编辑器存在一些错误,并且由于无符号变量下溢等问题而一次又一次地意外崩溃.发生这种情况时,将终端恢复到原始状态的清理代码将永远不会运行.

However, the editor has some bugs, and crashes unexpectedly every now and again due to issues like unsigned variable underflow. When this happens, the cleanup code which would restore the terminal to its original state never runs.

我要运行的清理功能如下:

The cleanup function I'd like to run is the following:

fn restore_orig_mode(editor_config: &EditorConfig) -> io::Result<()> {
    termios::tcsetattr(STDIN, termios::TCSAFLUSH, &editor_config.orig_termios)
}

推荐答案

尝试

Try catch_unwind. I haven't used it, so I cannot guarantee it works.

这篇关于注册功能以在Rust程序意外退出期间运行的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆