From eed2c7ae6ce7f36f2ea096372d139e23d774da44 Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Thu, 6 Aug 2020 01:41:42 +0900 Subject: [PATCH] concept: TryFromObject with message? --- vm/src/pyobject.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/vm/src/pyobject.rs b/vm/src/pyobject.rs index 4a4118aaa2..3469ad37d8 100644 --- a/vm/src/pyobject.rs +++ b/vm/src/pyobject.rs @@ -1145,6 +1145,24 @@ pub trait TryFromObject: Sized { fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult; } +pub trait TryFromObjectWithMessage: Sized { + type Error: ?Sized; + + fn try_from_object_with_message( + vm: &VirtualMachine, + obj: PyObjectRef, + error: Self::Error, + ) -> PyResult; + + fn default_error() -> Self::Error; +} + +impl TryFromObject for T where Self: Sized, T: TryFromObjectWithMessage, F: Fn(&VirtualMachine, PyObjectRef) -> PyBaseExceptionRef { + fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult { + Self::try_from_object_with_message(vm, obj, Self::default_error()) + } +} + /// Implemented by any type that can be returned from a built-in Python function. /// /// `IntoPyObject` has a blanket implementation for any built-in object payload,