@@ -355,11 +355,20 @@ where
355
355
/// If `T` implements `PyTryFrom`, we can convert `&PyAny` to `&T`.
356
356
///
357
357
/// This trait is similar to `std::convert::TryFrom`
358
+ #[ deprecated( since = "0.21.0" ) ]
358
359
pub trait PyTryFrom < ' v > : Sized + PyNativeType {
359
360
/// Cast from a concrete Python object type to PyObject.
361
+ #[ deprecated(
362
+ since = "0.21.0" ,
363
+ note = "use `value.downcast::<T>()` instead of `T::try_from(value)`"
364
+ ) ]
360
365
fn try_from < V : Into < & ' v PyAny > > ( value : V ) -> Result < & ' v Self , PyDowncastError < ' v > > ;
361
366
362
367
/// Cast from a concrete Python object type to PyObject. With exact type check.
368
+ #[ deprecated(
369
+ since = "0.21.0" ,
370
+ note = "use `value.downcast_exact::<T>()` instead of `T::try_from_exact(value)`"
371
+ ) ]
363
372
fn try_from_exact < V : Into < & ' v PyAny > > ( value : V ) -> Result < & ' v Self , PyDowncastError < ' v > > ;
364
373
365
374
/// Cast a PyAny to a specific type of PyObject. The caller must
@@ -368,11 +377,16 @@ pub trait PyTryFrom<'v>: Sized + PyNativeType {
368
377
/// # Safety
369
378
///
370
379
/// Callers must ensure that the type is valid or risk type confusion.
380
+ #[ deprecated(
381
+ since = "0.21.0" ,
382
+ note = "use `value.downcast_unchecked::<T>()` instead of `T::try_from_unchecked(value)`"
383
+ ) ]
371
384
unsafe fn try_from_unchecked < V : Into < & ' v PyAny > > ( value : V ) -> & ' v Self ;
372
385
}
373
386
374
387
/// Trait implemented by Python object types that allow a checked downcast.
375
388
/// This trait is similar to `std::convert::TryInto`
389
+ #[ deprecated( since = "0.21.0" ) ]
376
390
pub trait PyTryInto < T > : Sized {
377
391
/// Cast from PyObject to a concrete Python object type.
378
392
fn try_into ( & self ) -> Result < & T , PyDowncastError < ' _ > > ;
@@ -381,6 +395,7 @@ pub trait PyTryInto<T>: Sized {
381
395
fn try_into_exact ( & self ) -> Result < & T , PyDowncastError < ' _ > > ;
382
396
}
383
397
398
+ #[ allow( deprecated) ]
384
399
mod implementations {
385
400
use super :: * ;
386
401
@@ -555,47 +570,50 @@ mod test_no_clone {}
555
570
556
571
#[ cfg( test) ]
557
572
mod tests {
558
- use crate :: PyObject ;
559
-
560
- use super :: super :: PyTryFrom ;
561
- use crate :: types:: { IntoPyDict , PyAny , PyDict , PyList } ;
562
- use crate :: { Python , ToPyObject } ;
563
-
564
- #[ test]
565
- fn test_try_from ( ) {
566
- Python :: with_gil ( |py| {
567
- let list: & PyAny = vec ! [ 3 , 6 , 5 , 4 , 7 ] . to_object ( py) . into_ref ( py) ;
568
- let dict: & PyAny = vec ! [ ( "reverse" , true ) ] . into_py_dict ( py) . as_ref ( ) ;
569
-
570
- assert ! ( <PyList as PyTryFrom <' _>>:: try_from( list) . is_ok( ) ) ;
571
- assert ! ( <PyDict as PyTryFrom <' _>>:: try_from( dict) . is_ok( ) ) ;
572
-
573
- assert ! ( <PyAny as PyTryFrom <' _>>:: try_from( list) . is_ok( ) ) ;
574
- assert ! ( <PyAny as PyTryFrom <' _>>:: try_from( dict) . is_ok( ) ) ;
575
- } ) ;
576
- }
573
+ use crate :: { PyObject , Python } ;
574
+
575
+ #[ allow( deprecated) ]
576
+ mod deprecated {
577
+ use super :: super :: PyTryFrom ;
578
+ use crate :: types:: { IntoPyDict , PyAny , PyDict , PyList } ;
579
+ use crate :: { Python , ToPyObject } ;
580
+
581
+ #[ test]
582
+ fn test_try_from ( ) {
583
+ Python :: with_gil ( |py| {
584
+ let list: & PyAny = vec ! [ 3 , 6 , 5 , 4 , 7 ] . to_object ( py) . into_ref ( py) ;
585
+ let dict: & PyAny = vec ! [ ( "reverse" , true ) ] . into_py_dict ( py) . as_ref ( ) ;
586
+
587
+ assert ! ( <PyList as PyTryFrom <' _>>:: try_from( list) . is_ok( ) ) ;
588
+ assert ! ( <PyDict as PyTryFrom <' _>>:: try_from( dict) . is_ok( ) ) ;
589
+
590
+ assert ! ( <PyAny as PyTryFrom <' _>>:: try_from( list) . is_ok( ) ) ;
591
+ assert ! ( <PyAny as PyTryFrom <' _>>:: try_from( dict) . is_ok( ) ) ;
592
+ } ) ;
593
+ }
577
594
578
- #[ test]
579
- fn test_try_from_exact ( ) {
580
- Python :: with_gil ( |py| {
581
- let list: & PyAny = vec ! [ 3 , 6 , 5 , 4 , 7 ] . to_object ( py) . into_ref ( py) ;
582
- let dict: & PyAny = vec ! [ ( "reverse" , true ) ] . into_py_dict ( py) . as_ref ( ) ;
595
+ #[ test]
596
+ fn test_try_from_exact ( ) {
597
+ Python :: with_gil ( |py| {
598
+ let list: & PyAny = vec ! [ 3 , 6 , 5 , 4 , 7 ] . to_object ( py) . into_ref ( py) ;
599
+ let dict: & PyAny = vec ! [ ( "reverse" , true ) ] . into_py_dict ( py) . as_ref ( ) ;
583
600
584
- assert ! ( PyList :: try_from_exact( list) . is_ok( ) ) ;
585
- assert ! ( PyDict :: try_from_exact( dict) . is_ok( ) ) ;
601
+ assert ! ( PyList :: try_from_exact( list) . is_ok( ) ) ;
602
+ assert ! ( PyDict :: try_from_exact( dict) . is_ok( ) ) ;
586
603
587
- assert ! ( PyAny :: try_from_exact( list) . is_err( ) ) ;
588
- assert ! ( PyAny :: try_from_exact( dict) . is_err( ) ) ;
589
- } ) ;
590
- }
604
+ assert ! ( PyAny :: try_from_exact( list) . is_err( ) ) ;
605
+ assert ! ( PyAny :: try_from_exact( dict) . is_err( ) ) ;
606
+ } ) ;
607
+ }
591
608
592
- #[ test]
593
- fn test_try_from_unchecked ( ) {
594
- Python :: with_gil ( |py| {
595
- let list = PyList :: new ( py, [ 1 , 2 , 3 ] ) ;
596
- let val = unsafe { <PyList as PyTryFrom >:: try_from_unchecked ( list. as_ref ( ) ) } ;
597
- assert ! ( list. is( val) ) ;
598
- } ) ;
609
+ #[ test]
610
+ fn test_try_from_unchecked ( ) {
611
+ Python :: with_gil ( |py| {
612
+ let list = PyList :: new ( py, [ 1 , 2 , 3 ] ) ;
613
+ let val = unsafe { <PyList as PyTryFrom >:: try_from_unchecked ( list. as_ref ( ) ) } ;
614
+ assert ! ( list. is( val) ) ;
615
+ } ) ;
616
+ }
599
617
}
600
618
601
619
#[ test]
0 commit comments