pyo3-utils 0.4.0

Utilities for PyO3
Documentation
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
//! When you want to implement a pyclass that can obtain ownership of the internal value in a semantically compatible way,
//! you can use this module.
//!
//! Pay attention to this module's:
//!
//! - [PyWrapper]
//! - [PyWrapperSemverExt]

use std::convert::Infallible;
use std::error::Error;
use std::fmt::{Debug, Display, Formatter};
use std::mem::replace;
use std::ops::{Deref, DerefMut};

use parking_lot::{
    MappedRwLockReadGuard, MappedRwLockWriteGuard, RwLock, RwLockReadGuard, RwLockWriteGuard,
};
use pyo3::exceptions::PyRuntimeError;
use pyo3::PyErr;

const CONSUMED_ERROR_MSG: &str = "Already consumed";
const LOCK_ERROR_MSG: &str = "Already mutably borrowed";

/// This error indicates that the internal value has been consumed, i.e., its ownership has been moved out.
#[derive(Debug)]
pub struct ConsumedError;

impl Display for ConsumedError {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{CONSUMED_ERROR_MSG}")
    }
}

impl Error for ConsumedError {}

impl From<ConsumedError> for PyErr {
    fn from(_: ConsumedError) -> Self {
        PyRuntimeError::new_err(CONSUMED_ERROR_MSG)
    }
}

pub type ConsumedResult<T> = Result<T, ConsumedError>;

/// This error indicates that the internal value has already been mutably borrowed.
/// At this point, you must wait for other code to release the lock.
#[derive(Debug)]
pub struct LockError;

impl Display for LockError {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{LOCK_ERROR_MSG}")
    }
}

impl Error for LockError {}

impl From<LockError> for PyErr {
    fn from(_: LockError) -> Self {
        PyRuntimeError::new_err(LOCK_ERROR_MSG)
    }
}

pub type LockResult<T> = Result<T, LockError>;

/// Can only obtain alias references
pub type PyWrapperT0<T> = Result<T, Infallible>;
/// Can obtain alias references and mutable references
// TODO, FIXME, PERF: we have to use `RwLock` instead of `RefCell`,
// it's because pyo3 require `Sync`.
//
// We need wait for pyo3 `unsync`, see also:
// - <https://github.com/PyO3/pyo3/issues/4265#issuecomment-2348510879>
// - <https://github.com/pydantic/pydantic-core/pull/1556#issue-2694035224>
//
// ---
//
// use `parking_lot` instead of `std`, it's because `parking_lot` will not poisoned.
pub type PyWrapperT1<T> = RwLock<Result<T, Infallible>>;
/// Can obtain alias references, mutable references, and ownership
pub type PyWrapperT2<T> = RwLock<Result<T, ConsumedError>>;

mod sealed {
    use super::*;

    pub trait PyWrapperT {}

    impl<T> PyWrapperT for PyWrapperT0<T> {}
    impl<T> PyWrapperT for PyWrapperT1<T> {}
    impl<T> PyWrapperT for PyWrapperT2<T> {}

    pub trait SealedPyWrapper {}

    impl<T> SealedPyWrapper for PyWrapper<T> where T: PyWrapperT {}

    pub trait SealedMappableDeref {}

    impl<T: ?Sized> SealedMappableDeref for &T {}
    impl<T: ?Sized> SealedMappableDeref for RwLockReadGuard<'_, T> {}
    impl<T: ?Sized> SealedMappableDeref for MappedRwLockReadGuard<'_, T> {}

    pub trait SealedMappableDerefMut {}

    impl<T: ?Sized> SealedMappableDerefMut for &mut T {}
    impl<T: ?Sized> SealedMappableDerefMut for RwLockWriteGuard<'_, T> {}
    impl<T: ?Sized> SealedMappableDerefMut for MappedRwLockWriteGuard<'_, T> {}
}

trait RwLockExt {
    type T;

    fn try_read_ext(&self) -> LockResult<RwLockReadGuard<'_, Self::T>>;

    fn try_write_ext(&self) -> LockResult<RwLockWriteGuard<'_, Self::T>>;
}

impl<T> RwLockExt for RwLock<T> {
    type T = T;

    fn try_read_ext(&self) -> LockResult<RwLockReadGuard<'_, T>> {
        self.try_read().ok_or(LockError)
    }

    fn try_write_ext(&self) -> LockResult<RwLockWriteGuard<'_, T>> {
        self.try_write().ok_or(LockError)
    }
}

/// This trait provides compatibility between `&T` and [parking_lot::RwLockReadGuard]
pub trait MappableDeref<'a>: Deref + sealed::SealedMappableDeref {
    /// This method is similar to [parking_lot::RwLockReadGuard::map] and its sibling methods.
    fn map<U, F>(self, f: F) -> impl MappableDeref<'a, Target = U>
    where
        U: ?Sized + 'a,
        F: FnOnce(&Self::Target) -> &U;
}

impl<'a, T> MappableDeref<'a> for &'a T
where
    T: ?Sized,
{
    fn map<U, F>(self, f: F) -> impl MappableDeref<'a, Target = U>
    where
        U: ?Sized + 'a,
        F: FnOnce(&T) -> &U,
    {
        f(self)
    }
}

impl<'a, T> MappableDeref<'a> for MappedRwLockReadGuard<'a, T>
where
    T: ?Sized + 'a,
{
    fn map<U, F>(self, f: F) -> impl MappableDeref<'a, Target = U>
    where
        U: ?Sized + 'a,
        F: FnOnce(&T) -> &U,
    {
        MappedRwLockReadGuard::map(self, f)
    }
}

impl<'a, T> MappableDeref<'a> for RwLockReadGuard<'a, T>
where
    T: ?Sized + 'a,
{
    fn map<U, F>(self, f: F) -> impl MappableDeref<'a, Target = U>
    where
        U: ?Sized + 'a,
        F: FnOnce(&T) -> &U,
    {
        RwLockReadGuard::map(self, f)
    }
}

/// This trait provides compatibility between [&mut T] and [parking_lot::RwLockWriteGuard]
pub trait MappableDerefMut<'a>: DerefMut + sealed::SealedMappableDerefMut {
    /// This method is similar to [parking_lot::RwLockWriteGuard::map] and its sibling methods.
    fn map<U, F>(self, f: F) -> impl MappableDerefMut<'a, Target = U>
    where
        U: ?Sized + 'a,
        F: FnOnce(&mut Self::Target) -> &mut U;
}

impl<'a, T> MappableDerefMut<'a> for &'a mut T
where
    T: ?Sized,
{
    fn map<U, F>(self, f: F) -> impl MappableDerefMut<'a, Target = U>
    where
        U: ?Sized + 'a,
        F: FnOnce(&mut T) -> &mut U,
    {
        f(self)
    }
}

impl<'a, T> MappableDerefMut<'a> for MappedRwLockWriteGuard<'a, T>
where
    T: ?Sized + 'a,
{
    fn map<U, F>(self, f: F) -> impl MappableDerefMut<'a, Target = U>
    where
        U: ?Sized + 'a,
        F: FnOnce(&mut T) -> &mut U,
    {
        MappedRwLockWriteGuard::map(self, f)
    }
}

impl<'a, T> MappableDerefMut<'a> for RwLockWriteGuard<'a, T>
where
    T: ?Sized + 'a,
{
    fn map<U, F>(self, f: F) -> impl MappableDerefMut<'a, Target = U>
    where
        U: ?Sized + 'a,
        F: FnOnce(&mut T) -> &mut U,
    {
        RwLockWriteGuard::map(self, f)
    }
}

/// You can wrap the desired internal value in this structure to implement a pyclass that
/// can obtain ownership of the internal value.
///
/// # Example
/**
```rust
use pyo3::prelude::*;
use pyo3_utils::py_wrapper::{PyWrapper, PyWrapperT2};

struct Foo;

impl Foo {
    fn foo(self) {}
}

#[pyclass(frozen)]
#[non_exhaustive]
pub struct Bar(PyWrapper<PyWrapperT2<Foo>>);

#[pymethods]
impl Bar {
    // Normally you can directly use `&self` to get a reference
    // instead of using `Py<Self>::get` in a pymethod.
    // Here just for demonstration purposes.
    fn py_foo(slf: Py<Self>) -> PyResult<()> {
        slf.get().0.try_take_inner()??.foo();
        Ok(())
    }
}
```
*/
/// NOTE: For [`PyWrapper<T>`], changes from `T = [PyWrapperT0] -> [PyWrapperT1] -> [PyWrapperT2]`
/// will not be considered breaking changes.
///
/// - When the type is [PyWrapperT0], all methods are zero-cost abstractions.
/// - When the type changes to [PyWrapperT1], compatibility with [PyWrapperT0] is achieved by
///   implicitly calling other methods that acquire locks. These compatible methods will emit
///   deprecation warnings.
/// - When the type changes to [PyWrapperT2], compatibility with [PyWrapperT1] is achieved by
///   implicitly calling [Result::unwrap()] on other methods that return [Result]. These
///   compatible methods will emit deprecation warnings.
pub struct PyWrapper<T>
where
    T: sealed::PyWrapperT,
{
    inner: T,
}

impl<T> PyWrapper<PyWrapperT0<T>> {
    #[inline]
    pub fn new0(inner: T) -> Self {
        Self { inner: Ok(inner) }
    }

    #[inline]
    pub fn inner_ref(&self) -> impl MappableDeref<'_, Target = T> {
        // TODO, FIXME: use [Result::into_ok] instead (unstable for now)
        match self.inner.as_ref() {
            Ok(inner) => inner,
            // this arm cannot be omitted because of the reference,
            // see: <https://blog.rust-lang.org/2024/10/17/Rust-1.82.0.html#omitting-empty-types-in-pattern-matching>
            Err(infallible) => match *infallible {},
        }
    }

    #[inline]
    pub fn inner_mut(&mut self) -> impl MappableDerefMut<'_, Target = T> {
        // TODO, FIXME: use [Result::into_ok] instead (unstable for now)
        match self.inner.as_mut() {
            Ok(inner) => inner,
            Err(infallible) => match *infallible {},
        }
    }

    #[inline]
    pub fn into_inner(self) -> T {
        // TODO, FIXME: use [Result::into_ok] instead (unstable for now)
        let Ok(inner) = self.inner;
        inner
    }
}

impl<T> PyWrapper<PyWrapperT1<T>> {
    #[inline]
    pub fn new1(inner: T) -> Self {
        Self {
            inner: RwLock::new(Ok(inner)),
        }
    }

    pub fn lock_inner_ref(&self) -> LockResult<MappedRwLockReadGuard<'_, T>> {
        self.inner
            .try_read_ext()
            // TODO, FIXME: use [Result::into_ok] instead (unstable for now)
            .map(|guard| {
                RwLockReadGuard::map(guard, |inner| match inner.as_ref() {
                    Ok(inner) => inner,
                    Err(infallible) => match *infallible {},
                })
            })
    }

    pub fn lock_inner_mut(&self) -> LockResult<MappedRwLockWriteGuard<'_, T>> {
        self.inner
            .try_write_ext()
            // TODO, FIXME: use [Result::into_ok] instead (unstable for now)
            .map(|guard| {
                RwLockWriteGuard::map(guard, |inner| match inner.as_mut() {
                    Ok(inner) => inner,
                    Err(infallible) => match *infallible {},
                })
            })
    }

    pub fn into_inner(self) -> T {
        // TODO, FIXME: use [Result::into_ok] instead (unstable for now)
        let Ok(inner) = self.inner.into_inner();
        inner
    }

    /// # Panics
    ///
    /// Panics if the internal value has already been mutably borrowed.
    #[deprecated(note = "use `lock_inner_ref` instead")]
    pub fn inner_ref(&self) -> impl MappableDeref<'_, Target = T> {
        self.lock_inner_ref().expect(LOCK_ERROR_MSG)
    }

    /// # Panics
    ///
    /// Panics if the internal value has already been mutably borrowed.
    #[deprecated(note = "use `lock_inner_mut` instead")]
    pub fn inner_mut(&self) -> impl MappableDerefMut<'_, Target = T> {
        self.lock_inner_mut().expect(LOCK_ERROR_MSG)
    }
}

impl<T> PyWrapper<PyWrapperT2<T>> {
    #[inline]
    pub fn new2(inner: T) -> Self {
        Self {
            inner: RwLock::new(Ok(inner)),
        }
    }

    pub fn try_lock_inner_ref(&self) -> LockResult<ConsumedResult<MappedRwLockReadGuard<'_, T>>> {
        self.try_read().map(|guard| {
            if guard.is_err() {
                Err(ConsumedError)
            } else {
                // PEFR: it's ok to use [unwrap_unchecked], but i dont like unsafe block
                Ok(RwLockReadGuard::map(guard, |inner| inner.as_ref().unwrap()))
            }
        })
    }

    pub fn try_lock_inner_mut(&self) -> LockResult<ConsumedResult<MappedRwLockWriteGuard<'_, T>>> {
        self.try_write().map(|guard| {
            if guard.is_err() {
                Err(ConsumedError)
            } else {
                // PEFR: it's ok to use [unwrap_unchecked], but i dont like unsafe block
                Ok(RwLockWriteGuard::map(guard, |inner| {
                    inner.as_mut().unwrap()
                }))
            }
        })
    }

    pub fn try_take_inner(&self) -> LockResult<ConsumedResult<T>> {
        self.try_replace_inner(Err(ConsumedError))
    }

    /// similar to [std::mem::replace]
    pub fn try_replace_inner(&self, inner: ConsumedResult<T>) -> LockResult<ConsumedResult<T>> {
        self.try_write().map(|mut guard| {
            let result = guard.deref_mut();
            replace(result, inner)
        })
    }

    /// similar to [parking_lot::RwLock::try_read]
    pub fn try_read(&self) -> LockResult<RwLockReadGuard<'_, ConsumedResult<T>>> {
        self.inner.try_read_ext()
    }

    /// similar to [parking_lot::RwLock::try_write]
    pub fn try_write(&self) -> LockResult<RwLockWriteGuard<'_, ConsumedResult<T>>> {
        self.inner.try_write_ext()
    }

    pub fn try_into_inner(self) -> ConsumedResult<T> {
        self.inner.into_inner()
    }

    /// # Panics
    ///
    /// Panics if the internal value has already been consumed, i.e., its ownership has been moved out.
    #[deprecated(note = "use `try_lock_inner_ref` instead")]
    pub fn lock_inner_ref(&self) -> LockResult<MappedRwLockReadGuard<'_, T>> {
        self.try_lock_inner_ref()
            .map(|result| result.expect(CONSUMED_ERROR_MSG))
    }

    /// # Panics
    ///
    /// Panics if the internal value has already been consumed, i.e., its ownership has been moved out.
    #[deprecated(note = "use `try_lock_inner_mut` instead")]
    pub fn lock_inner_mut(&self) -> LockResult<MappedRwLockWriteGuard<'_, T>> {
        self.try_lock_inner_mut()
            .map(|result| result.expect(CONSUMED_ERROR_MSG))
    }

    /// # Panics
    ///
    /// Panics if the internal value has already been mutably borrowed or consumed.
    #[deprecated(note = "use `try_lock_inner_ref` instead")]
    pub fn inner_ref(&self) -> impl MappableDeref<'_, Target = T> {
        self.try_lock_inner_ref()
            .expect(LOCK_ERROR_MSG)
            .expect(CONSUMED_ERROR_MSG)
    }

    /// # Panics
    ///
    /// Panics if the internal value has already been mutably borrowed or consumed.
    #[deprecated(note = "use `try_lock_inner_mut` instead")]
    pub fn inner_mut(&self) -> impl MappableDerefMut<'_, Target = T> {
        self.try_lock_inner_mut()
            .expect(LOCK_ERROR_MSG)
            .expect(CONSUMED_ERROR_MSG)
    }

    /// # Panics
    ///
    /// Panics if the internal value has already been consumed, i.e., its ownership has been moved out.
    #[deprecated(note = "use `try_into_inner` instead")]
    pub fn into_inner(self) -> T {
        self.try_into_inner().expect(CONSUMED_ERROR_MSG)
    }
}

/// This trait allows you to handle [PyWrapperT0] and [PyWrapperT1] with the API of [PyWrapper]<[PyWrapperT2]>,
/// so you can write future-compatible code.
///
/// # NOTE
/// <div class="warning">
///
/// You must drop the returned [MappableDeref] and [MappableDerefMut], because for the implementations
/// of [PyWrapperT1] and [PyWrapperT2], they will hold internal locks.
///
/// </div>
pub trait PyWrapperSemverExt: sealed::SealedPyWrapper {
    type Wrapped;

    /// For implementations of [PyWrapper]::<[PyWrapperT1]> and ::<[PyWrapperT2]>, locks will be acquired
    fn inner_ref_semver(
        &self,
    ) -> LockResult<ConsumedResult<impl MappableDeref<'_, Target = Self::Wrapped>>>;
    /// For implementations of [PyWrapper]::<[PyWrapperT1]> and ::<[PyWrapperT2]>, locks will be acquired
    fn inner_mut_semver(
        &mut self,
    ) -> LockResult<ConsumedResult<impl MappableDerefMut<'_, Target = Self::Wrapped>>>;
    fn into_inner_semver(self) -> ConsumedResult<Self::Wrapped>;
}

impl<T> PyWrapperSemverExt for PyWrapper<PyWrapperT0<T>> {
    type Wrapped = T;

    fn inner_ref_semver(
        &self,
    ) -> LockResult<ConsumedResult<impl MappableDeref<'_, Target = Self::Wrapped>>> {
        Ok(Ok(self.inner_ref()))
    }

    fn inner_mut_semver(
        &mut self,
    ) -> LockResult<ConsumedResult<impl MappableDerefMut<'_, Target = Self::Wrapped>>> {
        Ok(Ok(self.inner_mut()))
    }

    fn into_inner_semver(self) -> ConsumedResult<Self::Wrapped> {
        Ok(self.into_inner())
    }
}

impl<T> PyWrapperSemverExt for PyWrapper<PyWrapperT1<T>> {
    type Wrapped = T;

    fn inner_ref_semver(
        &self,
    ) -> LockResult<ConsumedResult<impl MappableDeref<'_, Target = Self::Wrapped>>> {
        self.lock_inner_ref().map(Ok)
    }

    fn inner_mut_semver(
        &mut self,
    ) -> LockResult<ConsumedResult<impl MappableDerefMut<'_, Target = Self::Wrapped>>> {
        self.lock_inner_mut().map(Ok)
    }

    fn into_inner_semver(self) -> ConsumedResult<Self::Wrapped> {
        Ok(self.into_inner())
    }
}

impl<T> PyWrapperSemverExt for PyWrapper<PyWrapperT2<T>> {
    type Wrapped = T;

    fn inner_ref_semver(
        &self,
    ) -> LockResult<ConsumedResult<impl MappableDeref<'_, Target = Self::Wrapped>>> {
        self.try_lock_inner_ref()
    }

    fn inner_mut_semver(
        &mut self,
    ) -> LockResult<ConsumedResult<impl MappableDerefMut<'_, Target = Self::Wrapped>>> {
        self.try_lock_inner_mut()
    }

    fn into_inner_semver(self) -> ConsumedResult<Self::Wrapped> {
        self.try_into_inner()
    }
}