pub struct Probe<'a> { /* private fields */ }
Expand description
A struct that stores information about how to compile and run test programs.
The main functionality of probe_c_api
is implemented using the methods on
Probe
. The lifetime parameter is provided in order to allow closures to be
used for the compiler and run commands. If 'static
types implementing Fn
are used (e.g. function pointers), the lifetime may be 'static
.
Implementations§
Source§impl<'a> Probe<'a>
impl<'a> Probe<'a>
Sourcepub fn new<C, R>(
headers: Vec<String>,
work_dir: &Path,
compile_to: C,
run: R,
) -> Result<Probe<'a>, NewProbeError>
pub fn new<C, R>( headers: Vec<String>, work_dir: &Path, compile_to: C, run: R, ) -> Result<Probe<'a>, NewProbeError>
Construct a Probe
by specifying a work directory, a method to compile
a C program, and a method to run a C program.
The headers
argument is a vector of headers to include in every C
program written by this probe. Each header should have the <>
or ""
delimiters surrounding it.
The work_dir
argument should be a path to a directory where the probe
can read, write, and execute files. We could attempt to verify this, but
in practice there are too many platforms and security measures out
there. So it is up to the user to figure out the difference between a
failure of a test and an inability to run the test due to security
measures.
Files in the work_dir
are kept from colliding via random number
generator, which makes it possible to execute tests in parallel, in
practice.
The compile_to
argument is responsible for taking a source file
&Path
(the first argument) and producing a runnable program at another
&Path
(the second argument). This is roughly equivalent to the shell
script:
gcc -c $1 -o $2
compile_to
should yield a CommandResult
, which allows the exit
status to be checked, and provides the standard output and error for
debugging purposes.
The run
argument is responsible for running the process and yielding
its status and output, again as a CommandResult
.
FIXME! Suggestions for equivalent non-POSIX examples, especially anything relevant for Windows, are welcomed.
Sourcepub fn check_compile(&self, source: &str) -> CommandResult
pub fn check_compile(&self, source: &str) -> CommandResult
Write a byte slice to a file, then attempt to compile it.
This is not terribly useful, and is provided mostly for users who simply
want to reuse a closure that was used to construct the Probe
, as well
as for convenience and testing of probe-c-api
itself.
Sourcepub fn check_run(&self, source: &str) -> Result<CompileRunOutput>
pub fn check_run(&self, source: &str) -> Result<CompileRunOutput>
Write a byte slice to a file, then attempt to compile and run it.
Like check_compile
, this provides little value, but is available as a
minor convenience.
Sourcepub fn size_of(&self, type_: &str) -> CProbeResult<usize>
pub fn size_of(&self, type_: &str) -> CProbeResult<usize>
Get the size of a C type, in bytes.
Sourcepub fn align_of(&self, type_: &str) -> CProbeResult<usize>
pub fn align_of(&self, type_: &str) -> CProbeResult<usize>
Get the alignment of a C type, in bytes.
Note that this method depends on the compiler having implemented C11
alignment facilities (specifically stdalign.h
and alignof
).
Sourcepub fn is_defined_macro(&self, token: &str) -> CProbeResult<bool>
pub fn is_defined_macro(&self, token: &str) -> CProbeResult<bool>
Check to see if a macro is defined.
One obvious use for this is to check for macros that are intended to be
used with #ifdef
, e.g. macros that communicate configuration options
originally used to build the library.
A less obvious use is to check whether or not a constant or function has been implemented as a macro, for cases where this is not specified in the API documentation, or differs between library versions. In such cases, bindings may have to omit functionality provided by macros, or else implement such functionality via some special workaround.
Sourcepub fn is_signed(&self, type_: &str) -> CProbeResult<bool>
pub fn is_signed(&self, type_: &str) -> CProbeResult<bool>
Check to see if an integer type is signed or unsigned.