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
32pub mod metrics;
33
34/// Generated configuration types for use with the various `set_option` functions
35#[allow(clippy::all)]
36pub mod options;
37if_cfg_api_versions! {min = 710 =>
38    #[cfg(feature = "tenant-experimental")]
39    pub mod tenant;
40}
41
42#[cfg(any(feature = "recipes", feature = "recipes-leader-election"))]
43pub mod recipes;
44
45// Re-export metrics types for convenience
46pub use crate::metrics::TransactionMetrics;
47pub mod timekeeper;
48mod transaction;
49mod tuple_ext;
50
51pub mod tuple {
52    pub use crate::tuple_ext::*;
53    pub use foundationdb_tuple::*;
54}
55
56if_cfg_api_versions! {min = 510, max = 600 =>
57    pub use crate::cluster::Cluster;
58}
59
60pub use crate::database::*;
61pub use crate::error::FdbBindingError;
62pub use crate::error::{FdbError, FdbResult};
63pub use crate::keyselector::*;
64pub use crate::transaction::*;
65
66/// Initialize the FoundationDB Client API, this can only be called once per process.
67///
68/// # Returns
69///
70/// A `NetworkAutoStop` handle which must be dropped before the program exits.
71///
72/// # Safety
73///
74/// You *MUST* ensure drop is called on the returned object before the program exits.
75/// This is not required if the program is aborted.
76///
77/// This method used to be safe in version `0.4`. But because `drop` on the returned object
78/// might not be called before the program exits, it was found unsafe.
79///
80/// # Examples
81///
82/// ```rust
83/// let network = unsafe { foundationdb::boot() };
84/// // do some interesting things with the API...
85/// drop(network);
86/// ```
87///
88/// ```rust
89/// #[tokio::main]
90/// async fn main() {
91///     let network = unsafe { foundationdb::boot() };
92///     // do some interesting things with the API...
93///     drop(network);
94/// }
95/// ```
96pub unsafe fn boot() -> api::NetworkAutoStop {
97    let network_builder = api::FdbApiBuilder::default()
98        .build()
99        .expect("foundationdb API to be initialized");
100    network_builder.boot().expect("fdb network running")
101}
102
103/// Returns the default Fdb cluster configuration file path
104#[cfg(target_os = "linux")]
105pub fn default_config_path() -> &'static str {
106    "/etc/foundationdb/fdb.cluster"
107}
108
109/// Returns the default Fdb cluster configuration file path
110#[cfg(target_os = "macos")]
111pub fn default_config_path() -> &'static str {
112    "/usr/local/etc/foundationdb/fdb.cluster"
113}
114
115/// Returns the default Fdb cluster configuration file path
116#[cfg(target_os = "windows")]
117pub fn default_config_path() -> &'static str {
118    "C:/ProgramData/foundationdb/fdb.cluster"
119}
120
121/// slice::from_raw_parts assumes the pointer to be aligned and non-null.
122/// Since Rust nightly (mid-February 2024), it is enforced with debug asserts,
123/// but the FDBServer can return a null pointer if the slice is empty:
124/// ptr: 0x0, len: 0
125/// To avoid the assert, an empty slice is directly returned in that situation
126fn from_raw_fdb_slice<T, U: Into<usize>>(ptr: *const T, len: U) -> &'static [T] {
127    if ptr.is_null() {
128        return &[];
129    }
130    unsafe { std::slice::from_raw_parts(ptr, len.into()) }
131}