Skip to content

hack3rmann/for-sure

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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:

# struct Window { pub name: String }
# impl Window {
#     pub fn new(name: &str) -> Self {
#         Self { name: name.to_owned() }
#     }
# }
# 
# trait App {
#     fn open_window(&mut self, name: &str);
#     fn update(&mut self);
# }
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.

About

Wrapper around Option<T> but with Deref<Target = T> implementation.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages