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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
#![doc = include_str!("../README.md")]
//---------------------------------------------------------------------------------------------------- Docs
#![cfg_attr(docsrs, feature(doc_cfg))]
//---------------------------------------------------------------------------------------------------- Lints
#![forbid(
future_incompatible,
let_underscore,
break_with_label_and_loop,
coherence_leak_check,
deprecated,
duplicate_macro_attributes,
exported_private_dependencies,
for_loops_over_fallibles,
large_assignments,
overlapping_range_endpoints,
semicolon_in_expressions_from_macros,
redundant_semicolons,
unconditional_recursion,
unused_allocation,
unused_braces,
unused_doc_comments,
unused_labels,
unused_unsafe,
while_true,
keyword_idents,
missing_docs,
non_ascii_idents,
noop_method_call,
unreachable_pub,
single_use_lifetimes,
variant_size_differences,
unused_mut,
unsafe_code,
)]
#![deny(
clippy::all,
clippy::correctness,
clippy::suspicious,
clippy::style,
clippy::complexity,
clippy::perf,
clippy::pedantic,
clippy::restriction,
clippy::nursery,
clippy::cargo,
unused_comparisons,
nonstandard_style,
)]
#![allow(
clippy::single_char_lifetime_names,
clippy::implicit_return,
clippy::std_instead_of_alloc,
clippy::std_instead_of_core,
clippy::unwrap_used,
clippy::min_ident_chars,
clippy::absolute_paths,
clippy::missing_inline_in_public_items,
clippy::arithmetic_side_effects,
clippy::unwrap_in_result,
clippy::pattern_type_mismatch,
clippy::shadow_reuse,
clippy::shadow_unrelated,
clippy::missing_trait_methods,
clippy::pub_use,
clippy::pub_with_shorthand,
clippy::blanket_clippy_restriction_lints,
clippy::exhaustive_structs,
clippy::exhaustive_enums,
clippy::panic,
)]
//---------------------------------------------------------------------------------------------------- Mod
pub mod commit;
pub use commit::*;
pub mod info;
pub use info::*;
mod reader;
pub use reader::Reader;
mod writer;
pub use writer::Writer;
//---------------------------------------------------------------------------------------------------- Type alias.
/// An incrementing [`usize`] representing a new versions of data
///
/// In [`Commit`] objects, there is a [`Timestamp`] that represents that data's "version".
///
/// It is just an incrementing [`usize`] starting at 0.
///
/// Every time the [`Writer`] calls a commit operation like [`Writer::commit()`],
/// or [`Writer::overwrite()`] the data's [`Timestamp`] is incremented by `1`, thus
/// the timestamp is also how many commits there are.
///
/// An invariant that can be relied upon is that the [`Writer`] can
/// never "rebase" (as in, go back in time with their [`Commit`]) more
/// further back than the current [`Reader`]'s [`Timestamp`].
///
/// This means the [`Writer`]'s timestamp will _always_ be
/// greater than or equal to the [`Reader`]'s timestamp.
///
/// ## Example
/// ```rust
///
/// let v = vec![];
/// let (r, mut w) = someday::new::<Vec<&str>>(v);
///
/// // Writer writes some data, but does not commit.
/// w.add(|w, _| w.push("a"));
/// // Timestamp is still 0.
/// assert_eq!(w.timestamp(), 0);
///
/// w.add(|w, _| w.push("b"));
/// assert_eq!(w.timestamp(), 0);
///
/// w.add(|w, _| w.push("b"));
/// assert_eq!(w.timestamp(), 0);
///
/// // Now we commit.
/// w.commit();
/// assert_eq!(w.timestamp(), 1);
///
/// // We haven't pushed though, so
/// // readers will see timestamp of 0
/// assert_eq!(r.timestamp(), 0);
/// ```
pub type Timestamp = usize;
//---------------------------------------------------------------------------------------------------- Free functions
/// The default `Vec` capacity for the
/// `Patch`'s when using using `new()`.
pub(crate) const INIT_VEC_LEN: usize = 16;
#[inline]
#[must_use]
/// Create a new [`Writer`] & [`Reader`] pair
///
/// See their documentation for writing and reading functions.
///
/// This pre-allocates `16` capacity for the internal
/// [`Vec`]'s holding onto the `Patch`'s that have and
/// haven't been applied.
///
/// Use [`new_with_capacity()`] to set a custom capacity.
///
/// ## Example
/// ```rust
/// let (reader, mut writer) = someday::new::<String>("".into());
/// ```
pub fn new<T>(data: T) -> (Reader<T>, Writer<T>)
where
T: Clone,
{
new_internal::<T>(data, INIT_VEC_LEN)
}
#[inline]
#[must_use]
/// Create a new [`Writer`] & [`Reader`] pair with a specified `Patch` capacity
///
/// This is the same as [`new()`] although the
/// the input `capacity` determines how much capacity the
/// `Patch` vectors will start out with.
///
/// Use this if you are planning to [`Writer::add()`]
/// many `Patch`'s before [`Writer::commit()`]'ing, so that
/// the internal [`Vec`]'s don't need to reallocate so often.
///
/// ## Example
/// ```rust
/// // Can fit 128 functions without re-allocating.
/// let (r, mut w) = someday::new_with_capacity::<String>("".into(), 128);
/// assert_eq!(w.staged().capacity(), 128);
/// assert_eq!(w.committed_patches().capacity(), 128);
/// ```
pub fn new_with_capacity<T>(data: T, capacity: usize) -> (Reader<T>, Writer<T>)
where
T: Clone,
{
new_internal::<T>(data, capacity)
}
#[inline]
#[must_use]
/// Create a default [`Writer`] & [`Reader`] pair
///
/// This is the same as [`new()`] but it does not
/// require input data, it will generate your data using
/// [`Default::default()`].
///
/// ## Example
/// ```rust
/// let (r, mut w) = someday::default::<String>();
/// assert_eq!(*w.data(), "");
/// assert_eq!(r.head(), "");
/// ```
pub fn default<T>() -> (Reader<T>, Writer<T>)
where
T: Default + Clone,
{
new_internal::<T>(Default::default(), INIT_VEC_LEN)
}
#[must_use]
/// Create a default [`Writer`] & [`Reader`] pair with a specified `Patch` capacity
///
/// This is the same as [`default()`] combined with [`new_with_capacity()`].
///
/// ## Example
/// ```rust
/// // Can fit 128 functions without re-allocating.
/// let (r, mut w) = someday::default_with_capacity::<String>(128);
/// assert_eq!(w.staged().capacity(), 128);
/// assert_eq!(w.committed_patches().capacity(), 128);
/// ```
pub fn default_with_capacity<T>(capacity: usize) -> (Reader<T>, Writer<T>)
where
T: Default + Clone,
{
new_internal::<T>(Default::default(), capacity)
}
/// Internal generic functions used by all `new()` functions above.
fn new_internal<T>(data: T, capacity: usize) -> (Reader<T>, Writer<T>)
where
T: Clone,
{
use std::sync::{Arc,atomic::AtomicBool};
let local = CommitOwned { timestamp: 0, data };
let remote = Arc::new(local.clone());
let arc = Arc::new(arc_swap::ArcSwapAny::new(Arc::clone(&remote)));
let swapping = Arc::new(AtomicBool::new(false));
let reader = Reader {
arc: Arc::clone(&arc),
swapping: Arc::clone(&swapping),
};
let writer = Writer {
local: Some(local),
remote,
arc,
patches: Vec::with_capacity(capacity),
patches_old: Vec::with_capacity(capacity),
tags: std::collections::BTreeMap::new(),
swapping,
};
(reader, writer)
}