ext_php_rs/types/
payload.rs1use std::{
4 cell::{Ref, RefCell, RefMut},
5 ops::{Deref, DerefMut},
6};
7
8use crate::{exception::PhpException, zend::ce};
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
15#[non_exhaustive]
16pub enum BorrowError {
17 #[error("Cannot use object of class {0} while one of its methods modifies it")]
19 MutablyBorrowed(&'static str),
20 #[error("Cannot modify object of class {0} while it is in use")]
22 Borrowed(&'static str),
23 #[error("Object of class {0} is not initialized")]
26 Uninitialized(&'static str),
27}
28
29impl From<BorrowError> for PhpException {
30 fn from(err: BorrowError) -> Self {
31 Self::new(err.to_string(), 0, ce::error())
32 }
33}
34
35#[derive(Debug)]
39pub struct ClassRef<'a, T>(Ref<'a, T>);
40
41#[derive(Debug)]
45pub struct ClassRefMut<'a, T>(RefMut<'a, T>);
46
47impl<T> Deref for ClassRef<'_, T> {
48 type Target = T;
49
50 fn deref(&self) -> &T {
51 &self.0
52 }
53}
54
55impl<T> Deref for ClassRefMut<'_, T> {
56 type Target = T;
57
58 fn deref(&self) -> &T {
59 &self.0
60 }
61}
62
63impl<T> DerefMut for ClassRefMut<'_, T> {
64 fn deref_mut(&mut self) -> &mut T {
65 &mut self.0
66 }
67}
68
69pub(crate) fn borrow<'a, T>(
70 cell: &'a RefCell<Option<T>>,
71 class: &'static str,
72) -> Result<ClassRef<'a, T>, BorrowError> {
73 let value = cell
74 .try_borrow()
75 .map_err(|_| BorrowError::MutablyBorrowed(class))?;
76 Ref::filter_map(value, Option::as_ref)
77 .map(ClassRef)
78 .map_err(|_| BorrowError::Uninitialized(class))
79}
80
81pub(crate) fn borrow_mut<'a, T>(
82 cell: &'a RefCell<Option<T>>,
83 class: &'static str,
84) -> Result<ClassRefMut<'a, T>, BorrowError> {
85 let value = cell
86 .try_borrow_mut()
87 .map_err(|_| BorrowError::Borrowed(class))?;
88 RefMut::filter_map(value, Option::as_mut)
89 .map(ClassRefMut)
90 .map_err(|_| BorrowError::Uninitialized(class))
91}
92
93pub(crate) fn replace<T>(
94 cell: &RefCell<Option<T>>,
95 value: T,
96 class: &'static str,
97) -> Result<Option<T>, BorrowError> {
98 let mut slot = cell
99 .try_borrow_mut()
100 .map_err(|_| BorrowError::Borrowed(class))?;
101 Ok(slot.replace(value))
102}
103
104#[cfg(test)]
105mod tests {
106 use std::cell::RefCell;
107
108 use super::{BorrowError, borrow, borrow_mut, replace};
109
110 #[test]
111 fn shared_borrows_coexist_and_block_exclusive() {
112 let cell = RefCell::new(Some(7_u32));
113 let a = borrow(&cell, "Counter").expect("first shared borrow");
114 let b = borrow(&cell, "Counter").expect("second shared borrow");
115 assert_eq!(*a + *b, 14);
116 assert_eq!(
117 borrow_mut(&cell, "Counter").err(),
118 Some(BorrowError::Borrowed("Counter"))
119 );
120 }
121
122 #[test]
123 fn exclusive_borrow_blocks_everything_until_dropped() {
124 let cell = RefCell::new(Some(1_u32));
125 {
126 let mut value = borrow_mut(&cell, "Counter").expect("exclusive borrow");
127 *value = 2;
128 assert_eq!(
129 borrow(&cell, "Counter").err(),
130 Some(BorrowError::MutablyBorrowed("Counter"))
131 );
132 }
133 assert_eq!(borrow(&cell, "Counter").map(|value| *value), Ok(2));
134 }
135
136 #[test]
137 fn uninitialized_cell_refuses_borrows_until_replaced() {
138 let cell = RefCell::new(None::<u32>);
139 assert_eq!(
140 borrow(&cell, "Counter").err(),
141 Some(BorrowError::Uninitialized("Counter"))
142 );
143 assert_eq!(
144 borrow_mut(&cell, "Counter").err(),
145 Some(BorrowError::Uninitialized("Counter"))
146 );
147 assert_eq!(replace(&cell, 3, "Counter"), Ok(None));
148 assert_eq!(borrow(&cell, "Counter").map(|value| *value), Ok(3));
149 }
150
151 #[test]
152 fn replace_is_refused_while_borrowed() {
153 let cell = RefCell::new(Some(1_u32));
154 let _shared = borrow(&cell, "Counter");
155 assert_eq!(
156 replace(&cell, 2, "Counter"),
157 Err(BorrowError::Borrowed("Counter"))
158 );
159 }
160
161 #[test]
162 fn messages_name_the_class() {
163 assert_eq!(
164 BorrowError::MutablyBorrowed("Counter").to_string(),
165 "Cannot use object of class Counter while one of its methods modifies it"
166 );
167 assert_eq!(
168 BorrowError::Borrowed("Counter").to_string(),
169 "Cannot modify object of class Counter while it is in use"
170 );
171 assert_eq!(
172 BorrowError::Uninitialized("Counter").to_string(),
173 "Object of class Counter is not initialized"
174 );
175 }
176}