Skip to content
Myr

Add example to Error::downcast_ref

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by David Tolnay · committed by David Tolnay · parent 8235aa9 · Browse files · 1 file+35 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+35 added−0 removed

  • other:+35 added−0 removed

1 file+35 added−0 removed

modified:src/error.rs+35 added
147 unmodified lines
148 }149 }150151 /// Downcast this error object by reference.152 ///153 /// # Example154 ///155 /// ```156 /// # use anyhow::anyhow;157 /// # use std::fmt::{self, Display};158 /// # use std::task::Poll;159 /// #160 /// # #[derive(Debug)]161 /// # enum DataStoreError {162 /// # Censored(()),163 /// # }164 /// #165 /// # impl Display for DataStoreError {166 /// # fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {167 /// # unimplemented!()168 /// # }169 /// # }170 /// #171 /// # impl std::error::Error for DataStoreError {}172 /// #173 /// # const REDACTED_CONTENT: () = ();174 /// #175 /// # let error = anyhow!("...");176 /// # let root_cause = &error;177 /// #178 /// # let ret =179 /// // If the error was caused by redaction, then return a tombstone instead180 /// // of the content.181 /// match root_cause.downcast_ref::<DataStoreError>() {182 /// Some(DataStoreError::Censored(_)) => Ok(Poll::Ready(REDACTED_CONTENT)),183 /// None => Err(error),184 /// }185 /// # ;186 /// ```187 pub fn downcast_ref<E>(&self) -> Option<&E>188 where189 E: Display + Debug + Send + Sync + 'static,190 {
222 unmodified lines