for-sure

Crates.iofor-sure
lib.rsfor-sure
version0.1.1
created_at2025-07-14 15:59:35.003602+00
updated_at2025-07-14 17:39:53.407485+00
descriptionOption-like type but with Deref implementation
homepage
repositoryhttps://www.github.com/hack3rmann/for-sure
max_upload_size
id1751990
size45,275
Svyatoslav (hack3rmann)

documentation

README

The Almost type

Sometimes you need the optional type which is almost always is Some. The Almost type provides such functionality. It implements both Deref<Target = T> and DerefMut which panic if the value is uninitilized.

Use cases

It is actually really bad to use Almost instead of Option because it disables 'null-safety' of Rust. But in some use cases it may help a lot. For example, structs with two-factor inititalization:

use for_sure::prelude::*;

// first init factor writes `Nil` to `window`
#[derive(Default)]
struct MyApp {
    window: Almost<Window>,
}

// trait implementation which initializes `MyApp`
impl App for MyApp {
    // this function calls always when `MyApp` is initialized,
    // but we forced to use option-like types
    fn open_window(&mut self, name: &str) {
        // the second factor initializes window completely
        self.window = Value(Window::new(name));
    }

    fn update(&mut self) {
        // you can access window's methods (or fields) without
        // writing `.as_ref().unwrap()` everywhere
        println!("name: {}", self.window.name());
    }
}

Note that it will panic if Almost's value accessed uninitilized.

Default features

  • std - enables std library.
Commit count: 0

cargo fmt