# Replace Option\<T\> in ffi

**URL:** <https://users.rust-lang.org/t/replace-option-t-in-ffi/65993>\
**Category:** help\
**Created:** [October 16, 2021, 2:00pm UTC](https://users.rust-lang.org/t/replace-option-t-in-ffi/65993 "2021-10-16T14:00:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Caviar-X](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/caviar-x/32/21730_2.png) [@Caviar-X](https://users.rust-lang.org/u/Caviar-X)\
**Post date:** [October 16, 2021, 2:00pm UTC](https://users.rust-lang.org/t/replace-option-t-in-ffi/65993/1 "2021-10-16T14:00:06Z")

</div>

for example

```rust
#[no_mangle]
pub extern "C" fn rust_decode(string : Box<CStr>) -> Option<Vec<u8>> {...}

```

how to convert `Option<_>` into a FFI-safe type

---

<div class="post-metadata">

**Author:** ![alice](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/alice/32/9777_2.png) [@alice](https://users.rust-lang.org/u/alice)\
**Post date:** [October 16, 2021, 3:40pm UTC](https://users.rust-lang.org/t/replace-option-t-in-ffi/65993/2 "2021-10-16T15:40:43Z")

</div>

Well it depends on what is inside your `Option`. For example, if you have `Option<Box<T>>`, then that is already FFI safe and equivalent to a pointer to `T`, with `None` being null pointer.

The bigger problem is honestly the vector. Vectors are not FFI safe — option or not. In this case, you could create your own `VecParts` struct and mark it `#[repr(C)]` for FFI-safety.

```rust
#[repr(C)]
pub struct VecParts {
    pub ptr: *mut u8,
    pub len: usize,
    pub cap: usize,
}

```

You could use a null pointer and zeros for `None` here.

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [January 14, 2022, 3:40pm UTC](https://users.rust-lang.org/t/replace-option-t-in-ffi/65993/3 "2022-01-14T15:40:54Z")

</div>

Closed on January 14, 2022, 3:40pm UTC

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.
