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