简体   繁体   English

为什么在返回类型选项时返回 None<T> 当 T 不是引用时不返回空指针

[英]Why is returning None when of return type option<T> not return the null pointer when T is not a reference

The FFI section in the nomicon states that nomicon 中的 FFI 部分指出

The most common type that takes advantage of the nullable pointer optimization is Option<T> , where None corresponds to null .利用可空指针优化的最常见类型是Option<T> ,其中None对应于null So Option<extern "C" fn(c_int) -> c_int> is a correct way to represent a nullable function pointer using the C ABI (corresponding to the C type int (*)(int) ).因此Option<extern "C" fn(c_int) -> c_int>是使用 C ABI 表示可空函数指针的正确方法(对应于 C 类型int (*)(int) )。

However, this function does not return null instead returning an address.但是,此函数不返回null而是返回一个地址。

#[no_mangle]
pub extern "C" fn test() -> Option<u8> {
    None
}

Rust also issues this warning : Rust 也会发出这个警告:

warning: `extern` fn uses type `Option<u8>`, which is not FFI-safe
 --> src/lib.rs:2:29
  |
2 | pub extern "C" fn test() -> Option<u8> {
  |                             ^^^^^^^^^^ not FFI-safe
  |
  = note: `#[warn(improper_ctypes_definitions)]` on by default
  = help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
  = note: enum has no representation hint

Changing the return type to Option<Box<u8>> makes the function return null as expected,将返回类型更改为Option<Box<u8>>使函数按预期返回null

#[no_mangle]
pub extern "C" fn test2() -> Option<Box<u8>> {
    None
}

The functions are called from c++ using the following header file使用以下头文件从 C++ 调用这些函数

extern "C" {
    void * test();
    void * test2();
}

The following c++ code下面的C++代码

int main()
{
    cout << test() << endl;
    cout << test2() << endl;
}

prints :印刷 :

0x5555a09d1200
0

nullable pointer optimization.可空指针优化。

If the Option contains a pointer type, such as Box then the optimisation can be applied.如果Option包含指针类型,例如Box则可以应用优化。 A u8 cannot be null and is not a pointer, but a Box<u8> is an owned pointer to a u8 , so the optimisation works there. u8不能为空并且不是指针,但Box<u8>是指向u8的拥有指针,因此优化在那里起作用。

More generally, this optimisation is called "niche-filling" which also applies to a few non-pointer Rust types, notably enums .更一般地说,这种优化被称为“利基填充”,它也适用于一些非指针 Rust 类型,特别是enums In the general case this is not safe for FFI though, and therefore doesn't apply to data in #[repr(C)] structs except for pointer types.在一般情况下,这对 FFI 来说并不安全,因此不适用于#[repr(C)]结构中的数据,指针类型除外。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当返回类型不是指针时返回NULL - Returning a NULL when return type is not a pointer 当返回类型为int时为什么不能使用NULL - why can't return NULL be used when the return type is int 当函数的返回类型是引用时返回此指针(C++) - Returning this pointer when the return type of a function is a reference (C++) 当函数返回类型为bool时,为什么我不能在C ++ 14中返回共享指针? - Why can't I return a shared pointer in C++14 when the function return type is bool? 为什么我不能直接从返回指针的 function 中返回对指针的引用? - Why can't I directly return reference to a pointer from a function returning a pointer? 返回引用时返回错误 - Return error when returning a reference 当返回类型是向量容器时,是否有在 c++ 中返回 Null 的解决方法? - Is there a workaround for returning Null in c++ when the return type is a vector container? 为什么对于引用返回类型的函数,返回指针而不是变量本身是正确的 - Why returning pointer instead of variable itself is correct for functions with reference return type 为什么我不能返回typedef类型的引用 - Why I can't return reference of typedef'd type 什么时候在C ++中返回指针,标量和引用? - When to return a pointer, scalar and reference in C++?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM