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