Skip to main content

yoke/
lib.rs

1// This file is part of ICU4X. For terms of use, please see the file
2// called LICENSE at the top level of the ICU4X source tree
3// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4
5// https://github.com/unicode-org/icu4x/blob/main/documents/process/boilerplate.md#library-annotations
6#![cfg_attr(not(any(test, doc)), no_std)]
7#![cfg_attr(
8    not(test),
9    deny(
10        clippy::indexing_slicing,
11        clippy::unwrap_used,
12        clippy::expect_used,
13        clippy::panic,
14    )
15)]
16// #![warn(missing_docs)]
17
18//! This crate provides [`Yoke<Y, C>`][Yoke], which allows one to "yoke" (attach) a zero-copy deserialized
19//! object (say, a [`Cow<'a, str>`](alloc::borrow::Cow)) to the source it was deserialized from, (say, an [`Rc<[u8]>`](alloc::rc::Rc)),
20//! known in this crate as a "cart", producing a type that looks like `Yoke<Cow<'static, str>, Rc<[u8]>>`
21//! and can be moved around with impunity.
22//!
23//! Succinctly, this allows one to "erase" static lifetimes and turn them into dynamic ones, similarly
24//! to how `dyn` allows one to "erase" static types and turn them into dynamic ones.
25//!
26//! Most of the time the yokeable `Y` type will be some kind of zero-copy deserializable
27//! abstraction, potentially with an owned variant (like [`Cow`](alloc::borrow::Cow),
28//! [`ZeroVec`](https://docs.rs/zerovec), or an aggregate containing such types), and the cart `C` will be some smart pointer like
29//!   [`Box<T>`](alloc::boxed::Box), [`Rc<T>`](alloc::rc::Rc), or [`Arc<T>`](std::sync::Arc), potentially wrapped in an [`Option<T>`](Option).
30//!
31//! The key behind this crate is [`Yoke::get()`], where calling [`.get()`][Yoke::get] on a type like
32//! `Yoke<Cow<'static, str>, _>` will get you a short-lived `&'a Cow<'a, str>`, restricted to the
33//! lifetime of the borrow used during [`.get()`](Yoke::get). This is entirely safe since the `Cow` borrows from
34//! the cart type `C`, which cannot be interfered with as long as the `Yoke` is borrowed by [`.get()`](Yoke::get).
35//! [`.get()`](Yoke::get) protects access by essentially reifying the erased lifetime to a safe local one
36//! when necessary.
37//!
38//! See the documentation of [`Yoke`] for more details.
39
40// The lifetimes here are important for safety and explicitly writing
41// them out is good even when redundant
42#![allow(clippy::needless_lifetimes)]
43
44#[cfg(feature = "alloc")]
45extern crate alloc;
46
47pub mod cartable_ptr;
48pub mod either;
49#[cfg(feature = "alloc")]
50pub mod erased;
51mod kinda_sorta_dangling;
52mod macro_impls;
53mod utils;
54mod yoke;
55mod yokeable;
56#[cfg(feature = "zerofrom")]
57mod zero_from;
58
59#[cfg(feature = "derive")]
60pub use yoke_derive::Yokeable;
61
62pub use crate::yoke::{CloneableCart, Yoke};
63pub use crate::yokeable::Yokeable;
64
65#[cfg(feature = "zerofrom")]
66use zerofrom::ZeroFrom;