Skip to main content

Migrator

Struct Migrator 

Source
pub struct Migrator<T: Versioned + DeserializeOwned> { /* private fields */ }
Expand description

Schema migration pipeline for a Versioned type.

Add from → from + 1 steps with Migrator::step; the order in which they’re added does not matter — Migrator::run walks them in version order.

Migrator<T> is cheaply Clone (each step’s closure lives behind an Arc, so cloning is a handful of refcount bumps, not a deep copy) and Send + Sync whenever T is — which is what lets a Patch (crate::flush::Patch) closure retain its own copy of the migrator and re-run it against the document read fresh on the shared I/O worker thread, instead of the stale, possibly-out-of-date value this handle loaded at construction.

Implementations§

Source§

impl<T: Versioned + DeserializeOwned> Migrator<T>

Source

pub fn new() -> Self

Create an empty migrator with no steps registered.

If T::CURRENT_VERSION is 1 (the initial schema) or the file is already at the current version, no steps are needed and run will succeed immediately.

Source

pub fn step<F>(self, from: u32, func: F) -> Self
where F: Fn(Value) -> Result<Value, String> + Send + Sync + 'static,

Register a step that promotes a value from from to from + 1. Steps may be registered in any order; run finds the right one for the current version on demand.

Source

pub fn run(&self, raw: Value) -> Result<T, MigrationError>

Migrate raw from its on-disk version up to T::CURRENT_VERSION, then deserialize.

Reads the version directly from the version field of the raw toml::Value — never deserializes-then-checks, because a v1 payload typically fails to deserialize as the v2 type.

Files missing the version field are treated as v1 (legacy).

Trait Implementations§

Source§

impl<T: Versioned + DeserializeOwned> Clone for Migrator<T>

Source§

fn clone(&self) -> Self

Hand-written rather than #[derive(Clone)]: a derive would add a spurious T: Clone bound (from the PhantomData<T> field) even though nothing here actually needs it — Vec<Step> clones just fine on its own (each Step::func is an Arc, so this is a handful of refcount bumps).

1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Versioned + DeserializeOwned> Debug for Migrator<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Versioned + DeserializeOwned> Default for Migrator<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for Migrator<T>

§

impl<T> !UnwindSafe for Migrator<T>

§

impl<T> Freeze for Migrator<T>

§

impl<T> Send for Migrator<T>
where T: Send,

§

impl<T> Sync for Migrator<T>
where T: Sync,

§

impl<T> Unpin for Migrator<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for Migrator<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.