Skip to content

Commit 74fe024

Browse files
committed
Add an example to the WithTls and WithoutTls EnvOpenOptions functions
1 parent 5eeecfc commit 74fe024

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

heed/src/envs/env_open_options.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,29 @@ impl<T: TlsUsage> EnvOpenOptions<T> {
6464
/// child (nested) transactions. Each transaction belongs to one
6565
/// thread. A `BadRslot` error will be thrown when multiple read
6666
/// transactions exists on the same thread.
67+
///
68+
/// # Example
69+
///
70+
/// This example shows that the `RoTxn<'_, WithTls>` cannot be sent between threads.
71+
///
72+
/// ```compile_fail
73+
/// use std::fs;
74+
/// use std::path::Path;
75+
/// use heed::{EnvOpenOptions, Database, EnvFlags};
76+
/// use heed::types::*;
77+
///
78+
/// /// Checks, at compile time, that a type can be sent accross threads.
79+
/// fn is_sendable<S: Send>(_x: S) {}
80+
///
81+
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
82+
/// let mut env_builder = EnvOpenOptions::new().read_txn_with_tls();
83+
/// let dir = tempfile::tempdir().unwrap();
84+
/// let env = unsafe { env_builder.open(dir.path())? };
85+
///
86+
/// let rtxn = env.read_txn()?;
87+
/// is_sendable(rtxn);
88+
/// # Ok(()) }
89+
/// ```
6790
pub fn read_txn_with_tls(self) -> EnvOpenOptions<WithTls> {
6891
let Self { map_size, max_readers, max_dbs, flags, _tls_marker: _ } = self;
6992
EnvOpenOptions { map_size, max_readers, max_dbs, flags, _tls_marker: PhantomData }
@@ -75,6 +98,30 @@ impl<T: TlsUsage> EnvOpenOptions<T> {
7598
/// A thread can use any number of read transactions at a time on
7699
/// the same thread. Read transactions can be moved in between
77100
/// threads (`Send`).
101+
///
102+
///
103+
/// # Example
104+
///
105+
/// This example shows that the `RoTxn<'_, WithoutTls>` can be sent between threads.
106+
///
107+
/// ```
108+
/// use std::fs;
109+
/// use std::path::Path;
110+
/// use heed::{EnvOpenOptions, Database, EnvFlags};
111+
/// use heed::types::*;
112+
///
113+
/// /// Checks, at compile time, that a type can be sent accross threads.
114+
/// fn is_sendable<S: Send>(_x: S) {}
115+
///
116+
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
117+
/// let mut env_builder = EnvOpenOptions::new().read_txn_without_tls();
118+
/// let dir = tempfile::tempdir().unwrap();
119+
/// let env = unsafe { env_builder.open(dir.path())? };
120+
///
121+
/// let rtxn = env.read_txn()?;
122+
/// is_sendable(rtxn);
123+
/// # Ok(()) }
124+
/// ```
78125
pub fn read_txn_without_tls(self) -> EnvOpenOptions<WithoutTls> {
79126
let Self { map_size, max_readers, max_dbs, flags, _tls_marker: _ } = self;
80127
EnvOpenOptions { map_size, max_readers, max_dbs, flags, _tls_marker: PhantomData }

0 commit comments

Comments
 (0)