@@ -7,7 +7,7 @@ use crate::{
77 exceptions:: { self , PyBaseException } ,
88 ffi,
99} ;
10- use crate :: { IntoPy , Py , PyAny , PyNativeType , PyObject , Python , ToPyObject } ;
10+ use crate :: { Borrowed , IntoPy , Py , PyAny , PyNativeType , PyObject , Python , ToPyObject } ;
1111use std:: borrow:: Cow ;
1212use std:: cell:: UnsafeCell ;
1313use std:: ffi:: CString ;
@@ -46,34 +46,47 @@ unsafe impl Sync for PyErr {}
4646pub type PyResult < T > = Result < T , PyErr > ;
4747
4848/// Error that indicates a failure to convert a PyAny to a more specific Python type.
49- #[ derive( Debug ) ]
50- pub struct PyDowncastError < ' a > {
51- from : & ' a PyAny ,
52- to : Cow < ' static , str > ,
53- }
49+ pub struct PyDowncastError < ' py > ( DowncastError < ' py , ' py > ) ;
5450
5551impl < ' a > PyDowncastError < ' a > {
5652 /// Create a new `PyDowncastError` representing a failure to convert the object
5753 /// `from` into the type named in `to`.
5854 pub fn new ( from : & ' a PyAny , to : impl Into < Cow < ' static , str > > ) -> Self {
59- PyDowncastError {
60- from,
61- to : to. into ( ) ,
62- }
55+ PyDowncastError ( DowncastError :: new_from_borrowed ( from. as_borrowed ( ) , to) )
56+ }
57+ }
58+
59+ impl std:: fmt:: Debug for PyDowncastError < ' _ > {
60+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
61+ f. debug_struct ( "PyDowncastError" )
62+ . field ( "from" , & self . 0 . from )
63+ . field ( "to" , & self . 0 . to )
64+ . finish ( )
6365 }
6466}
6567
6668/// Error that indicates a failure to convert a PyAny to a more specific Python type.
6769#[ derive( Debug ) ]
6870pub struct DowncastError < ' a , ' py > {
69- from : & ' a Bound < ' py , PyAny > ,
71+ from : Borrowed < ' a , ' py , PyAny > ,
7072 to : Cow < ' static , str > ,
7173}
7274
7375impl < ' a , ' py > DowncastError < ' a , ' py > {
7476 /// Create a new `PyDowncastError` representing a failure to convert the object
7577 /// `from` into the type named in `to`.
7678 pub fn new ( from : & ' a Bound < ' py , PyAny > , to : impl Into < Cow < ' static , str > > ) -> Self {
79+ DowncastError {
80+ from : from. as_borrowed ( ) ,
81+ to : to. into ( ) ,
82+ }
83+ }
84+
85+ /// Similar to [`DowncastError::new`], but from a `Borrowed` instead of a `Bound`.
86+ pub ( crate ) fn new_from_borrowed (
87+ from : Borrowed < ' a , ' py , PyAny > ,
88+ to : impl Into < Cow < ' static , str > > ,
89+ ) -> Self {
7790 DowncastError {
7891 from,
7992 to : to. into ( ) ,
@@ -829,20 +842,15 @@ impl PyErrArguments for PyDowncastErrorArguments {
829842/// Convert `PyDowncastError` to Python `TypeError`.
830843impl < ' a > std:: convert:: From < PyDowncastError < ' a > > for PyErr {
831844 fn from ( err : PyDowncastError < ' _ > ) -> PyErr {
832- let args = PyDowncastErrorArguments {
833- from : err. from . get_type ( ) . into ( ) ,
834- to : err. to ,
835- } ;
836-
837- exceptions:: PyTypeError :: new_err ( args)
845+ PyErr :: from ( err. 0 )
838846 }
839847}
840848
841849impl < ' a > std:: error:: Error for PyDowncastError < ' a > { }
842850
843851impl < ' a > std:: fmt:: Display for PyDowncastError < ' a > {
844852 fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> Result < ( ) , std:: fmt:: Error > {
845- display_downcast_error ( f , & self . from . as_borrowed ( ) , & self . to )
853+ self . 0 . fmt ( f )
846854 }
847855}
848856
@@ -882,13 +890,13 @@ impl std::error::Error for DowncastIntoError<'_> {}
882890
883891impl std:: fmt:: Display for DowncastIntoError < ' _ > {
884892 fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> Result < ( ) , std:: fmt:: Error > {
885- display_downcast_error ( f, & self . from , & self . to )
893+ display_downcast_error ( f, self . from . as_borrowed ( ) , & self . to )
886894 }
887895}
888896
889897fn display_downcast_error (
890898 f : & mut std:: fmt:: Formatter < ' _ > ,
891- from : & Bound < ' _ , PyAny > ,
899+ from : Borrowed < ' _ , ' _ , PyAny > ,
892900 to : & str ,
893901) -> std:: fmt:: Result {
894902 write ! (
0 commit comments