1212//! If you need mutability in the node’s `data`,
1313//! make it a cell (`Cell` or `RefCell`) or use cells inside of it.
1414
15- use std:: cell:: Cell ;
15+ use std:: cell:: { BorrowError , Cell , Ref , RefCell , RefMut } ;
1616use std:: fmt;
1717
1818/// A node inside a DOM-like tree.
@@ -29,12 +29,12 @@ pub struct Node<'a, T: 'a> {
2929
3030/// A simple Debug implementation that prints the children as a tree, without
3131/// looping through the various interior pointer cycles.
32- impl < ' a , T : ' a > fmt:: Debug for Node < ' a , T >
32+ impl < ' a , T : ' a > fmt:: Debug for Node < ' a , RefCell < T > >
3333where
3434 T : fmt:: Debug ,
3535{
3636 fn fmt ( & self , f : & mut fmt:: Formatter ) -> Result < ( ) , fmt:: Error > {
37- struct Children < ' a , T > ( Option < & ' a Node < ' a , T > > ) ;
37+ struct Children < ' a , T > ( Option < & ' a Node < ' a , RefCell < T > > > ) ;
3838 impl < T : fmt:: Debug > fmt:: Debug for Children < ' _ , T > {
3939 fn fmt ( & self , f : & mut fmt:: Formatter ) -> Result < ( ) , fmt:: Error > {
4040 f. debug_list ( )
@@ -45,11 +45,14 @@ where
4545 }
4646 }
4747
48- let mut struct_fmt = f. debug_struct ( "Node" ) ;
49- struct_fmt. field ( "data" , & self . data ) ;
50- struct_fmt. field ( "children" , & Children ( self . first_child . get ( ) ) ) ;
51- struct_fmt. finish ( ) ?;
52-
48+ if let Ok ( data) = self . data . try_borrow ( ) {
49+ write ! ( f, "{data:?}" ) ?;
50+ } else {
51+ write ! ( f, "!!mutably borrowed!!" ) ?;
52+ }
53+ if let Some ( first_child) = self . first_child . get ( ) {
54+ write ! ( f, " {:?}" , & Children ( Some ( first_child) ) ) ?;
55+ }
5356 Ok ( ( ) )
5457 }
5558}
@@ -259,9 +262,17 @@ impl<'a, T> Node<'a, T> {
259262macro_rules! axis_iterator {
260263 ( #[ $attr: meta] $name: ident : $next: ident) => {
261264 #[ $attr]
262- #[ derive( Debug ) ]
263265 pub struct $name<' a, T : ' a>( Option <& ' a Node <' a, T >>) ;
264266
267+ impl <' a, T : ' a> fmt:: Debug for $name<' a, RefCell <T >>
268+ where
269+ T : fmt:: Debug ,
270+ {
271+ fn fmt( & self , f: & mut fmt:: Formatter <' _>) -> fmt:: Result {
272+ f. debug_tuple( stringify!( $name) ) . field( & self . 0 ) . finish( )
273+ }
274+ }
275+
265276 impl <' a, T > Iterator for $name<' a, T > {
266277 type Item = & ' a Node <' a, T >;
267278
@@ -304,9 +315,17 @@ axis_iterator! {
304315}
305316
306317/// An iterator of references to a given node and its descendants, in tree order.
307- #[ derive( Debug ) ]
308318pub struct Descendants < ' a , T : ' a > ( Traverse < ' a , T > ) ;
309319
320+ impl < ' a , T : ' a > fmt:: Debug for Descendants < ' a , RefCell < T > >
321+ where
322+ T : fmt:: Debug ,
323+ {
324+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
325+ f. debug_tuple ( "Descendants" ) . field ( & self . 0 ) . finish ( )
326+ }
327+ }
328+
310329impl < ' a , T > Iterator for Descendants < ' a , T > {
311330 type Item = & ' a Node < ' a , T > ;
312331
@@ -338,12 +357,23 @@ pub enum NodeEdge<T> {
338357macro_rules! traverse_iterator {
339358 ( #[ $attr: meta] $name: ident : $first_child: ident, $next_sibling: ident) => {
340359 #[ $attr]
341- #[ derive( Debug ) ]
342360 pub struct $name<' a, T : ' a> {
343361 root: & ' a Node <' a, T >,
344362 next: Option <NodeEdge <& ' a Node <' a, T >>>,
345363 }
346364
365+ impl <' a, T : ' a> fmt:: Debug for $name<' a, RefCell <T >>
366+ where
367+ T : fmt:: Debug ,
368+ {
369+ fn fmt( & self , f: & mut fmt:: Formatter <' _>) -> fmt:: Result {
370+ f. debug_struct( stringify!( $name) )
371+ . field( "root" , & self . root)
372+ . field( "next" , & self . next)
373+ . finish( )
374+ }
375+ }
376+
347377 impl <' a, T > Iterator for $name<' a, T > {
348378 type Item = NodeEdge <& ' a Node <' a, T >>;
349379
@@ -434,19 +464,19 @@ fn it_works() {
434464 assert_eq ! ( drop_counter. get( ) , 10 ) ;
435465}
436466
437- impl < ' a , T > Node < ' a , std :: cell :: RefCell < T > > {
467+ impl < ' a , T > Node < ' a , RefCell < T > > {
438468 /// Shorthand for `node.data.borrow()`.
439- pub fn data ( & self ) -> std :: cell :: Ref < ' _ , T > {
469+ pub fn data ( & self ) -> Ref < ' _ , T > {
440470 self . data . borrow ( )
441471 }
442472
443473 /// Shorthand for `node.data.try_borrow()`.
444- pub fn try_data ( & self ) -> Result < std :: cell :: Ref < ' _ , T > , std :: cell :: BorrowError > {
474+ pub fn try_data ( & self ) -> Result < Ref < ' _ , T > , BorrowError > {
445475 self . data . try_borrow ( )
446476 }
447477
448478 /// Shorthand for `node.data.borrow_mut()`.
449- pub fn data_mut ( & self ) -> std :: cell :: RefMut < ' _ , T > {
479+ pub fn data_mut ( & self ) -> RefMut < ' _ , T > {
450480 self . data . borrow_mut ( )
451481 }
452482}
0 commit comments