1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-03-30 01:16:17 +02:00
comprehensive-rust/src/exercises/day-3/safe-ffi-wrapper.md
2022-12-21 16:38:28 +01:00

1.5 KiB

Safe FFI Wrapper

Rust has great support for calling functions through a foreign function interface (FFI). We will use this to build a safe wrapper the glibc functions you would use from C to read the filenames of a directory.

You will want to consult the manual pages:

You will also want to browse the std::ffi module, particular for CStr and CString types which are used to hold NUL-terminated strings coming from C. The Nomicon also has a very useful chapter about FFI.

Copy the code below to https://play.rust-lang.org/ and fill in the missing functions and methods:

// TODO: remove this when you're done with your implementation.
#![allow(unused_imports, unused_variables, dead_code)]

{{#include safe-ffi-wrapper.rs:ffi}}

{{#include safe-ffi-wrapper.rs:DirectoryIterator}}
        unimplemented!()
    }
}

{{#include safe-ffi-wrapper.rs:Iterator}}
        unimplemented!()
    }
}

{{#include safe-ffi-wrapper.rs:Drop}}
        unimplemented!()
    }
}

{{#include safe-ffi-wrapper.rs:main}}