foundationdb/
lib.rs

1// Copyright 2018 foundationdb-rs developers, https://github.com/Clikengo/foundationdb-rs/graphs/contributors
2//
3// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
4// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5// http://opensource.org/licenses/MIT>, at your option. This file may not be
6// copied, modified, or distributed except according to those terms.
7#![doc = include_str!("../README.md")]
8
9use foundationdb_sys::if_cfg_api_versions;
10
11#[macro_use]
12extern crate static_assertions;
13
14pub mod api;
15if_cfg_api_versions! {min = 510, max = 600 =>
16    pub mod cluster;
17}
18mod database;
19pub mod directory;
20mod error;
21if_cfg_api_versions! {min = 700 =>
22    #[deny(missing_docs)]
23    pub mod fdb_keys;
24}
25pub mod future;
26mod keyselector;
27if_cfg_api_versions! {min = 710 =>
28    #[deny(missing_docs)]
29    pub mod mapped_key_values;
30}
31/// Generated configuration types for use with the various `set_option` functions
32#[allow(clippy::all)]
33pub mod options;
34if_cfg_api_versions! {min = 710 =>
35    #[cfg(feature = "tenant-experimental")]
36    pub mod tenant;
37}
38pub mod timekeeper;
39mod transaction;
40mod tuple_ext;
41
42pub mod tuple {
43    pub use crate::tuple_ext::*;
44    pub use foundationdb_tuple::*;
45}
46
47if_cfg_api_versions! {min = 510, max = 600 =>
48    pub use crate::cluster::Cluster;
49}
50
51pub use crate::database::*;
52pub use crate::error::FdbBindingError;
53pub use crate::error::FdbError;
54pub use crate::error::FdbResult;
55pub use crate::keyselector::*;
56pub use crate::transaction::*;
57
58/// Initialize the FoundationDB Client API, this can only be called once per process.
59///
60/// # Returns
61///
62/// A `NetworkAutoStop` handle which must be dropped before the program exits.
63///
64/// # Safety
65///
66/// You *MUST* ensure drop is called on the returned object before the program exits.
67/// This is not required if the program is aborted.
68///
69/// This method used to be safe in version `0.4`. But because `drop` on the returned object
70/// might not be called before the program exits, it was found unsafe.
71///
72/// # Examples
73///
74/// ```rust
75/// let network = unsafe { foundationdb::boot() };
76/// // do some interesting things with the API...
77/// drop(network);
78/// ```
79///
80/// ```rust
81/// #[tokio::main]
82/// async fn main() {
83///     let network = unsafe { foundationdb::boot() };
84///     // do some interesting things with the API...
85///     drop(network);
86/// }
87/// ```
88pub unsafe fn boot() -> api::NetworkAutoStop {
89    let network_builder = api::FdbApiBuilder::default()
90        .build()
91        .expect("foundationdb API to be initialized");
92    network_builder.boot().expect("fdb network running")
93}
94
95/// Returns the default Fdb cluster configuration file path
96#[cfg(target_os = "linux")]
97pub fn default_config_path() -> &'static str {
98    "/etc/foundationdb/fdb.cluster"
99}
100
101/// Returns the default Fdb cluster configuration file path
102#[cfg(target_os = "macos")]
103pub fn default_config_path() -> &'static str {
104    "/usr/local/etc/foundationdb/fdb.cluster"
105}
106
107/// Returns the default Fdb cluster configuration file path
108#[cfg(target_os = "windows")]
109pub fn default_config_path() -> &'static str {
110    "C:/ProgramData/foundationdb/fdb.cluster"
111}
112
113/// slice::from_raw_parts assumes the pointer to be aligned and non-null.
114/// Since Rust nightly (mid-February 2024), it is enforced with debug asserts,
115/// but the FDBServer can return a null pointer if the slice is empty:
116/// ptr: 0x0, len: 0
117/// To avoid the assert, an empty slice is directly returned in that situation
118fn from_raw_fdb_slice<T, U: Into<usize>>(ptr: *const T, len: U) -> &'static [T] {
119    if ptr.is_null() {
120        return &[];
121    }
122    unsafe { std::slice::from_raw_parts(ptr, len.into()) }
123}