forked from oxigraph/oxigraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
131 lines (117 loc) · 4.19 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#![doc = include_str!("../README.md")]
#![doc(html_favicon_url = "https://raw.githubusercontent.com/oxigraph/oxigraph/main/logo.svg")]
#![doc(html_logo_url = "https://raw.githubusercontent.com/oxigraph/oxigraph/main/logo.svg")]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
#![doc(test(attr(deny(warnings))))]
#![allow(clippy::return_self_not_must_use)]
pub mod io;
pub mod sparql;
mod storage;
pub mod store;
pub mod model {
//! Implements data structures for [RDF 1.1 Concepts](https://www.w3.org/TR/rdf11-concepts/) using [OxRDF](https://crates.io/crates/oxrdf).
pub use oxrdf::{
dataset, graph, vocab, BlankNode, BlankNodeIdParseError, BlankNodeRef, Dataset, Graph,
GraphName, GraphNameRef, IriParseError, LanguageTagParseError, Literal, LiteralRef,
NamedNode, NamedNodeRef, NamedOrBlankNode, NamedOrBlankNodeRef, Quad, QuadRef, Subject,
SubjectRef, Term, TermParseError, TermRef, Triple, TripleRef,
};
}
#[cfg(feature = "internal-rng")]
use core::time::Duration;
#[cfg(feature = "internal-rng")]
use getrandom::{register_custom_getrandom, Error};
#[cfg(feature = "internal-rng")]
use ic_cdk::export::candid;
#[cfg(feature = "internal-rng")]
use rand::Rng;
use rand::{rngs::StdRng, SeedableRng};
use std::cell::RefCell;
// This part of code in which we register a custom getrandom function is taken from https://github.com/demergent-labs/cdk_framework/blob/7e913d7ac49affad4a0bd5ee24b51b1a5d5d6096/src/act/random.rs
thread_local! {
/* flexible */ static RNG_REF_CELL: RefCell<StdRng> = RefCell::new(SeedableRng::from_seed([0_u8; 32]));
}
#[cfg(feature = "internal-rng")]
fn custom_getrandom(buf: &mut [u8]) -> Result<(), Error> {
RNG_REF_CELL.with(|rng_ref_cell| {
let mut rng = rng_ref_cell.borrow_mut();
rng.fill(buf);
});
Ok(())
}
#[cfg(feature = "internal-rng")]
fn rng_seed() {
ic_cdk::spawn(async move {
let result: ic_cdk::api::call::CallResult<(Vec<u8>,)> =
ic_cdk::api::call::call(candid::Principal::management_canister(), "raw_rand", ()).await;
RNG_REF_CELL.with(|rng_ref_cell| {
let mut rng = rng_ref_cell.borrow_mut();
match result {
Ok(randomness) => {
*rng = SeedableRng::from_seed(randomness.0[..].try_into().unwrap())
}
Err(err) => panic!("{:?}", err),
};
});
});
}
#[cfg(feature = "internal-rng")]
register_custom_getrandom!(custom_getrandom);
/// Pass the **Random Number Generator** as a RefCell.
///
/// This function **must** be called in the `init` and `post_upgrade` functions of the canister that imports this library.
///
/// # Example
/// ```rust
/// use ic_cdk_macros::{init, post_upgrade};
/// use ic_oxigraph;
///
/// thread_local! {
/// // Feed the RNG with a seed of 32 bytes and pass this reference to the library.
/// // See lines above for an example of how to obtain a random seed.
/// /* flexible */ static RNG_REF_CELL: RefCell<StdRng> = RefCell::new(SeedableRng::from_seed([0_u8; 32]));
/// }
///
/// #[init]
/// fn init() {
/// RNG_REF_CELL.with(ic_oxigraph::init);
/// // other init code
/// }
///
/// #[post_upgrade]
/// fn post_upgrade() {
/// RNG_REF_CELL.with(ic_oxigraph::init);
/// // other post_upgrade code like loading the stable memory into the state
/// }
/// ```
#[cfg(not(feature = "internal-rng"))]
pub fn init(rng: &RefCell<StdRng>) {
RNG_REF_CELL.with(|rng_ref_cell| {
*rng_ref_cell.borrow_mut() = rng.borrow().clone();
});
}
/// Initializes the **Random Number Generator** by asynchronously calling the management canister to obtain a random seed.
///
/// This function **must** be called in the `init` and `post_upgrade` functions of the canister that imports this library.
///
/// # Example
/// ```rust
/// use ic_cdk_macros::{init, post_upgrade};
/// use ic_oxigraph;
///
/// #[init]
/// fn init() {
/// ic_oxigraph::init();
/// // other init code
/// }
///
/// #[post_upgrade]
/// fn post_upgrade() {
/// ic_oxigraph::init();
/// // other post_upgrade code like loading the stable memory into the state
/// }
/// ```
#[cfg(feature = "internal-rng")]
pub fn init() {
ic_cdk_timers::set_timer(Duration::new(0, 0), rng_seed);
}