From 3f750e1b6ba6bb2395574d24645fa2c36b8e92ab Mon Sep 17 00:00:00 2001 From: jack-cooper Date: Fri, 23 Dec 2022 21:42:59 +0000 Subject: [PATCH 1/2] Fixed `LateInit` example stack overflow --- src/lib.rs | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 2f6cdac..3a06600 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -206,6 +206,8 @@ //! //! //! ``` +//! use std::fmt; +//! //! use once_cell::sync::OnceCell; //! //! #[derive(Debug)] @@ -228,16 +230,28 @@ //! } //! } //! -//! #[derive(Default, Debug)] +//! #[derive(Default)] //! struct A<'a> { //! b: LateInit<&'a B<'a>>, //! } //! -//! #[derive(Default, Debug)] +//! #[derive(Default)] //! struct B<'a> { //! a: LateInit<&'a A<'a>> //! } //! +//! impl fmt::Debug for A<'_> { +//! fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { +//! write!(f, "A") +//! } +//! } +//! +//! impl fmt::Debug for B<'_> { +//! fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { +//! write!(f, "B") +//! } +//! } +//! //! fn build_cycle() { //! let a = A::default(); //! let b = B::default(); @@ -314,9 +328,9 @@ //! **Does this crate support async?** //! //! No, but you can use [`async_once_cell`](https://crates.io/crates/async_once_cell) instead. -//! +//! //! **Can I bring my own mutex?** -//! +//! //! There is [generic_once_cell](https://crates.io/crates/generic_once_cell) to allow just that. //! //! # Related crates From e94003a2993923601cac5c1ba0b6988e13a6af39 Mon Sep 17 00:00:00 2001 From: jack-cooper Date: Sat, 24 Dec 2022 16:02:10 +0000 Subject: [PATCH 2/2] Removed Debug impls to de-clutter example --- src/lib.rs | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 3a06600..83149ac 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -206,11 +206,8 @@ //! //! //! ``` -//! use std::fmt; -//! //! use once_cell::sync::OnceCell; //! -//! #[derive(Debug)] //! pub struct LateInit { cell: OnceCell } //! //! impl LateInit { @@ -240,24 +237,14 @@ //! a: LateInit<&'a A<'a>> //! } //! -//! impl fmt::Debug for A<'_> { -//! fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { -//! write!(f, "A") -//! } -//! } -//! -//! impl fmt::Debug for B<'_> { -//! fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { -//! write!(f, "B") -//! } -//! } //! //! fn build_cycle() { //! let a = A::default(); //! let b = B::default(); //! a.b.init(&b); //! b.a.init(&a); -//! println!("{:?}", a.b.a.b.a); +//! +//! let _a = &a.b.a.b.a; //! } //! ``` //!