Skip to content

Instantly share code, notes, and snippets.

@orzklv
Created January 8, 2026 21:01
Show Gist options
  • Select an option

  • Save orzklv/fa3ad6f64cc9028afeca81685478daf9 to your computer and use it in GitHub Desktop.

Select an option

Save orzklv/fa3ad6f64cc9028afeca81685478daf9 to your computer and use it in GitHub Desktop.
Preserve self instance while applying a closure function.
/// Apply closure function preserving self instance
pub trait TryTap: Sized {
fn try_tap<E>(self, f: impl FnOnce(&Self) -> Result<(), E>) -> Result<Self, E>;
}
impl<T> TryTap for T {
fn try_tap<E>(self, f: impl FnOnce(&Self) -> Result<(), E>) -> Result<Self, E> {
f(&self)?;
Ok(self)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment