musli/context/
mod.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
//! [`Context`] implementations.
//!
//! [`Context`]: crate::Context

mod access;
use self::access::{Access, Shared};

mod error_marker;
#[doc(inline)]
pub use self::error_marker::ErrorMarker;

mod default_context;
#[doc(inline)]
pub use self::default_context::{DefaultContext, Error};

mod context_error;
#[doc(inline)]
pub use self::context_error::ContextError;

mod same;
#[doc(inline)]
pub use self::same::Same;

mod capture;
#[doc(inline)]
pub use self::capture::Capture;

mod ignore;
#[doc(inline)]
pub use self::ignore::Ignore;

use crate::alloc::Allocator;
#[cfg(feature = "alloc")]
use crate::alloc::System;

/// Construct a new default context using the provided allocator.
///
/// # Examples
///
/// ```
/// use musli::context;
///
/// musli::alloc::default!(|alloc| {
///     let cx = context::with_alloc(alloc);
///     let encoding = musli::json::Encoding::new();
///     let string = encoding.to_string_with(&cx, &42)?;
///     assert_eq!(string, "42");
/// });
/// # Ok::<(), musli::context::ErrorMarker>(())
/// ```
pub fn with_alloc<'a, A, M>(alloc: &'a A) -> DefaultContext<'a, A, M>
where
    A: 'a + ?Sized + Allocator,
    M: 'static,
{
    DefaultContext::with_alloc(alloc)
}

/// Construct a new default context using the static [`System`] allocator.
///
/// [`System`]: crate::alloc::System
///
/// # Examples
///
/// ```
/// use musli::context;
///
/// musli::alloc::default!(|alloc| {
///     let cx = context::new();
///     let encoding = musli::json::Encoding::new();
///     let string = encoding.to_string_with(&cx, &42)?;
///     assert_eq!(string, "42");
/// });
/// # Ok::<(), musli::context::ErrorMarker>(())
/// ```
#[cfg(feature = "alloc")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))]
pub fn new<M>() -> DefaultContext<'static, System, M>
where
    M: 'static,
{
    DefaultContext::new()
}