From 9af75bae41c354b9c127790d4adb600fea7a533c Mon Sep 17 00:00:00 2001 From: HalidOdat Date: Tue, 25 Aug 2020 21:09:29 +0200 Subject: [PATCH] Implement `std:error::Error` for Borrow errors --- boa/src/builtins/object/gcobject.rs | 31 +++++++++++++++++------------ 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/boa/src/builtins/object/gcobject.rs b/boa/src/builtins/object/gcobject.rs index 59dc448e657..836dd9b63f0 100644 --- a/boa/src/builtins/object/gcobject.rs +++ b/boa/src/builtins/object/gcobject.rs @@ -14,11 +14,12 @@ use crate::{ Executable, Interpreter, Result, }; use gc::{Finalize, Gc, GcCell, GcCellRef, GcCellRefMut, Trace}; -use std::result::Result as StdResult; use std::{ cell::RefCell, collections::HashSet, + error::Error, fmt::{self, Debug, Display}, + result::Result as StdResult, }; /// Garbage collected `Object`. @@ -223,8 +224,23 @@ impl Display for BorrowError { } } -#[derive(Debug)] +impl Error for BorrowError {} + +/// An error returned by [`GcObject::try_borrow_mut`](struct.GcObject.html#method.try_borrow_mut). +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct BorrowMutError; + +impl Display for BorrowMutError { + #[inline] + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + Display::fmt("Object already borrowed", f) + } +} + +impl Error for BorrowMutError {} + /// Prevents infinite recursion during `Debug::fmt`. +#[derive(Debug)] struct RecursionLimiter { /// If this was the first `GcObject` in the tree. free: bool, @@ -292,14 +308,3 @@ impl Debug for GcObject { } } } - -/// An error returned by [`GcObject::try_borrow_mut`](struct.GcObject.html#method.try_borrow_mut). -#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct BorrowMutError; - -impl Display for BorrowMutError { - #[inline] - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - Display::fmt("Object already borrowed", f) - } -}