Trait Hatchable

Source
pub trait Hatchable<T, E> {
    // Required method
    fn hatch(self) -> Hatch<T>;
}
Expand description

Extension trait for mapping other Result<T, E> types into Hatch<T> easily.

This trait enables seamless integration between the Yoshi error ecosystem and external error types. It provides the .hatch() method that converts any Result with an error type that can be converted to Yoshi into a Hatch<T>.

§Type Requirements

The error type E must implement Into<Yoshi> to enable conversion. This is automatically satisfied for:

  • std::io::Error (when std feature is enabled)
  • NoStdIo (when std feature is disabled)
  • String and &str types
  • Any type that implements std::error::Error + Send + Sync + 'static

§Performance Characteristics

  • Conversion Cost: O(1) for types with direct Into<Yoshi> implementations
  • Memory Overhead: Minimal - reuses existing error allocation where possible
  • Type Safety: Compile-time guarantees with no runtime type checking

§Examples

use yoshi_std::{Hatch, Hatchable, LayContext};

fn file_operation() -> Hatch<String> {
    std::fs::read_to_string("config.toml")
        .hatch()  // Convert io::Error to Yoshi
        .lay("While reading configuration file")
}

fn parse_operation() -> Hatch<i32> {
    "not_a_number".parse::<i32>()
        .map_err(|e| e.to_string())  // Convert to String first
        .hatch()  // Then convert to Yoshi
        .lay("While parsing user input")
}

Required Methods§

Source

fn hatch(self) -> Hatch<T>

Converts an error into a Hatch<T> by mapping it into Yoshi.

This method provides a convenient way to bring external error types into the Yoshi ecosystem while maintaining type safety and performance efficiency. The conversion leverages existing Into<Yoshi> implementations to minimize overhead and maintain semantic meaning.

§Type Conversion Chain

The method works by applying the following transformation: Result<T, E>Result<T, Yoshi> (via E: Into<Yoshi>)

§Returns

A Hatch<T> containing either the original success value or the converted error.

§Performance Considerations
  • Zero-cost for compatible types: When E already has efficient Into<Yoshi>
  • Minimal allocation: Reuses existing error data structures where possible
  • Compile-time optimization: Fully optimizable conversion chains
§Examples
use yoshi_std::{Hatch, Hatchable};

// I/O error conversion
let io_result: Result<String, io::Error> = Err(io::Error::new(
    io::ErrorKind::NotFound, "file not found"
));
let hatched: Hatch<String> = io_result.hatch();
assert!(hatched.is_err());

// String error conversion
let string_result: Result<i32, String> = Err("parsing failed".to_string());
let hatched: Hatch<i32> = string_result.hatch();
assert!(hatched.is_err());
§Errors

Returns a Hatch<T> containing the converted error if self is Err, or the original success value if self is Ok. Conversion errors are not possible as the Into<Yoshi> bound guarantees valid transformation.

Implementors§

Source§

impl<T, E: Into<Yoshi>> Hatchable<T, E> for Result<T, E>