Skip to main content

rm2k/
owned.rs

1//! Detaching a loaded file from the buffer it borrows.
2//!
3//! Loading is zero-copy: a `Database<'a>` holds `&'a [u8]` slices pointing into the bytes you
4//! read off disk, so parsing a 4 MB `RPG_RT.ldb` copies none of its text.
5//!
6//! ```
7//! # use rm2k::{file, rpg::Database, owned::IntoOwned, Engine, SaveOpt, LcfStruct};
8//! # let mut bytes = Vec::new();
9//! # file::save_database(&Database::defaults(Engine::R2K), &mut bytes,
10//! #                     Engine::R2K, SaveOpt::default(), b"").unwrap();
11//! fn load_it(bytes: Vec<u8>) -> Database<'static> {
12//!     let db = file::load_database(&bytes).unwrap();
13//!     db.value.into_owned()
14//! }
15//! # let _ = load_it(bytes);
16//! ```
17
18use crate::field::{BitArray, DbStr, List, Packed, PackedPrim, UnindexedList};
19use alloc::vec::Vec;
20
21/// A value that can be detached from the buffer it borrows.
22pub trait IntoOwned {
23    /// The same shape, owning its data.
24    type Owned: 'static;
25
26    /// Copy anything still borrowed, so the result outlives the input buffer.
27    fn into_owned(self) -> Self::Owned;
28}
29
30/// Implement a no-op detach for types that never borrow.
31#[macro_export]
32macro_rules! impl_identity_owned {
33    ($($ty:ty),* $(,)?) => {
34        $(
35            impl $crate::owned::IntoOwned for $ty {
36                type Owned = Self;
37                #[inline]
38                fn into_owned(self) -> Self {
39                    self
40                }
41            }
42        )*
43    };
44}
45
46impl_identity_owned!(i8, u8, i16, u32, i32, f64, bool);
47
48impl IntoOwned for DbStr<'_> {
49    type Owned = DbStr<'static>;
50    #[inline]
51    fn into_owned(self) -> DbStr<'static> {
52        DbStr::into_owned(self)
53    }
54}
55
56impl IntoOwned for BitArray<'_> {
57    type Owned = BitArray<'static>;
58    #[inline]
59    fn into_owned(self) -> BitArray<'static> {
60        BitArray::into_owned(self)
61    }
62}
63
64impl<T: PackedPrim + 'static> IntoOwned for Packed<T> {
65    type Owned = Self;
66    #[inline]
67    fn into_owned(self) -> Self {
68        self
69    }
70}
71
72impl<T: IntoOwned> IntoOwned for Vec<T> {
73    type Owned = Vec<T::Owned>;
74    #[inline]
75    fn into_owned(self) -> Vec<T::Owned> {
76        self.into_iter().map(IntoOwned::into_owned).collect()
77    }
78}
79
80impl<T: IntoOwned> IntoOwned for List<T> {
81    type Owned = List<T::Owned>;
82    #[inline]
83    fn into_owned(self) -> List<T::Owned> {
84        List(self.0.into_owned())
85    }
86}
87
88impl<T: IntoOwned> IntoOwned for UnindexedList<T> {
89    type Owned = UnindexedList<T::Owned>;
90    #[inline]
91    fn into_owned(self) -> UnindexedList<T::Owned> {
92        UnindexedList(self.0.into_owned())
93    }
94}
95
96// The raw codecs: `Rect`, `Equipment` and `Parameters` own their data outright, while the three
97// command/string lists borrow.
98impl_identity_owned!(
99    crate::raw::EmptyBlock,
100    crate::raw::DatabaseVersion,
101    crate::raw::Rect,
102    crate::raw::Equipment,
103    crate::raw::Parameters,
104);
105
106impl IntoOwned for crate::raw::EventCommand<'_> {
107    type Owned = crate::raw::EventCommand<'static>;
108    fn into_owned(self) -> Self::Owned {
109        crate::raw::EventCommand {
110            code: self.code,
111            indent: self.indent,
112            string: self.string.into_owned(),
113            parameters: self.parameters,
114        }
115    }
116}
117
118impl IntoOwned for crate::raw::EventCommandList<'_> {
119    type Owned = crate::raw::EventCommandList<'static>;
120    fn into_owned(self) -> Self::Owned {
121        crate::raw::EventCommandList(self.0.into_owned())
122    }
123}
124
125impl IntoOwned for crate::raw::MoveCommand<'_> {
126    type Owned = crate::raw::MoveCommand<'static>;
127    fn into_owned(self) -> Self::Owned {
128        crate::raw::MoveCommand {
129            code: self.code,
130            string: self.string.into_owned(),
131            parameter_a: self.parameter_a,
132            parameter_b: self.parameter_b,
133            parameter_c: self.parameter_c,
134        }
135    }
136}
137
138impl IntoOwned for crate::raw::MoveCommandList<'_> {
139    type Owned = crate::raw::MoveCommandList<'static>;
140    fn into_owned(self) -> Self::Owned {
141        crate::raw::MoveCommandList(self.0.into_owned())
142    }
143}
144
145impl IntoOwned for crate::raw::StrList<'_> {
146    type Owned = crate::raw::StrList<'static>;
147    fn into_owned(self) -> Self::Owned {
148        crate::raw::StrList(self.0.into_owned())
149    }
150}
151
152impl IntoOwned for crate::rpg::TreeMap<'_> {
153    type Owned = crate::rpg::TreeMap<'static>;
154    fn into_owned(self) -> Self::Owned {
155        crate::rpg::TreeMap {
156            maps: self.maps.into_owned(),
157            tree_order: self.tree_order,
158            active_node: self.active_node,
159            start: self.start,
160        }
161    }
162}