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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
//! Arena-aware analogs of [`From`] / [`Into`].
//!
//! Several `std` `From` impls (e.g. `From<&[T]> for Vec<T>`,
//! `From<&str> for String`) cannot be mirrored directly because they allocate
//! "from nothing" and thus require an allocator. [`FromIn`] is the arena-aware
//! counterpart: it threads the arena (`&'a Arena<A>`) through, exactly as
//! [`FromIteratorIn`](crate::vec::FromIteratorIn) does for
//! [`FromIterator`](core::iter::FromIterator).
//!
//! [`IntoIn`] is the [`Into`]-style companion, blanket-implemented for every
//! type convertible via [`FromIn`].
//!
//! ```
//! use multitude::vec::Vec;
//! use multitude::{Arena, FromIn, IntoIn};
//!
//! let arena = Arena::new();
//!
//! // Via `FromIn`:
//! let v: Vec<u32> = Vec::from_in([1_u32, 2, 3], &arena);
//! assert_eq!(&*v, &[1, 2, 3]);
//!
//! // Via `IntoIn` (the target type drives inference):
//! let w: Vec<u32> = [4_u32, 5].into_in(&arena);
//! assert_eq!(&*w, &[4, 5]);
//! ```
use ;
use crateArena;
/// Arena-aware counterpart to [`From`].
///
/// Builds `Self` from `value`, allocating into `arena`. Use this for
/// conversions that `std` exposes as [`From`] but that need an arena to
/// materialize the result.
/// Arena-aware counterpart to [`Into`].
///
/// Provides `.into_in(arena)` for any `T` convertible into `C` via [`FromIn`],
/// through the conditional blanket impl below; the target collection `C` is
/// usually pinned by a type annotation. As with [`std::convert::Into`], you may
/// also implement `IntoIn` directly when the corresponding [`FromIn`] impl is
/// blocked by the orphan rule (e.g. converting a local type into a foreign
/// one), since the blanket only applies when `C: FromIn<Self>`.
///
/// ```
/// use multitude::strings::String;
/// use multitude::{Arena, IntoIn};
///
/// let arena = Arena::new();
/// let s: String = "hello".into_in(&arena);
/// assert_eq!(s.as_str(), "hello");
/// ```
// Mirrors `impl<T, U: From<T>> Into<U> for T`: the blanket is conditional on
// `C: FromIn<'a, Self, A>`, so a `(Self, C)` pair whose `FromIn` is blocked by
// the orphan rule may still hand-implement `IntoIn` without colliding here.