是否可以将类型 `std::result::Result` 转换为 `minhook::Hook`? [英] Is it possible to cast type `std::result::Result` to `minhook::Hook`?

查看:36
本文介绍了是否可以将类型 `std::result::Result` 转换为 `minhook::Hook`?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用 minhook 库?

Is it possible to cast type std::result::Result to minhook::Hook using the minhook library?

test =
    unsafe {
        minhook::Hook::<fn(f32, *mut UserCmd) -> bool>::create::<fn(f32, *mut UserCmd) -> bool>(hook_createmove, fn_ptrs.addy)
    } as minhook::Hook<fn(f32, *mut UserCmd) -> bool>;

minhook::Hook::::create 返回一个 std::result::Result

如您所见,这给我带来了非标量转换问题.有解决方法吗?

As you can see, this is giving me the non-scalar cast problem. Is there a workaround for this?

推荐答案

Result 是 Rust 中表示操作成功或失败的标准类型.它的声明大致如下:

Result<T, E> is the standard type for representing success or failure of an operation in Rust. It's declared roughly like this:

enum Result<T, E> {
    Ok(T),
    Err(E),
}

任何可能以相当可恢复的方式失败的函数(例如:尝试打开不存在的文件或读取损坏的 JSON 文件时发生解析错误)通常会返回 Result.另请参阅使用结果有什么好处?.

Any function that could fail in a fairly recoverable way (examples: attempting to open a file that does not exist or a parsing error occurred when reading a corrupted JSON file) will usually return a Result. See also What's the benefit of using a Result?.

您应该不要尝试重新解释结果对象.铸造是不可能的,你绝对不应该使用嬗变.相反,您应该始终(我的意思是始终)使用适当的方法来处理它们.指出只是结果处理的几个例子:

You should not attempt to reinterpret result objects. Casting is not possible, and you should definitely not use transmutation. Instead, you should always (and I do mean always) use proper methods for handling them. Pointing out just a few examples of result handling:

  • 要果断地检索正面结果(T 类型的对象),请使用 unwrap() 方法.如果结果有错误,程序就会恐慌.当您确定在特定情况下不会发生错误时,或者当您确实希望程序在发生这种情况时终止时,通常会使用此方法.

  • To assertively retrieve the positive outcome (the object of type T), use the unwrap() method. The program will panic if the result holds an error. This is often used when you are sure that no errors can happen in that particular case, or when you really want the program to terminate when that happens.

在其他一些情况下,您希望从函数中传播错误.这可以通过 ? 运算符.

In some other cases, you want to propagate an error out of a function. This is most elegantly achieved with the ? operator.

Rust 编程语言提供了关于 错误处理,强烈建议 Rust 开发人员理解这些概念.

The Rust Programming language provides a comprehensive section on Error Handling, and Rust developers are strongly encouraged to understand these concepts.

这篇关于是否可以将类型 `std::result::Result` 转换为 `minhook::Hook`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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