Giter Club home page Giter Club logo

turbonone's Introduction

crates.io docs.rs license

turbonone (no_std)

Tiny macro for calling functions with generic Option<T> arguments.

Usage

Add to your Cargo.toml file:

[dependencies]
turbonone = "0.*"

The Problem

fn my_function<T>(arg: Option<T>) -> &'static str {
    "Works!"
}

fn my_box_function<T>(arg: Option<Box<T>>) -> &'static str {
    "Works!"
}

fn my_complex_function<T>(arg: Option<Arc<Box<T>>>) -> &'static str {
    "Works!"
}

my_function(None); // cannot infer type for type parameter `T` declared on the associated function `my_function`
my_function(Some("An argument")); // Works!

my_box_function(None); // cannot infer type for type parameter `T` declared on the associated function `my_box_function`
my_box_function(Some(Box::new("An argument"))); // Works!

my_complex_function(None); // cannot infer type for type parameter `T` declared on the associated function `my_complex_function`
my_complex_function(Some(Arc::new(Box::new("An argument")))); // Works!

The Solution

#[macro_use] extern crate turbonone;

fn my_function<T>(arg: Option<T>) -> &'static str {
    "Works!"
}

fn my_box_function<T>(arg: Option<Box<T>>) -> &'static str {
    "Works!"
}

fn my_complex_function<T>(arg: Option<Arc<Box<T>>>) -> &'static str {
    "Works!"
}

my_function(turbonone!()); // Works!
my_function(Some("An argument")); // Works!

my_box_function(turbonone!(Box)); // Works!
my_box_function(turbonone!(Box<()>)); // Works!
my_box_function(Some(Box::new("An argument"))); // Works!

my_complex_function(turbonone!(Arc<Box<()>>)); // Works!
my_complex_function(Some(Arc::new(Box::new("An argument")))); // Works!

turbonone's People

Contributors

williamvenner avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

turbonone's Issues

Doesn't work with closures

I have a function similar to this

fn foo<F>(f: Option<F>) 
where F: Fn(i32) -> Vec<i32>

Calling it with turbonone

foo(turbonone!());

gives me a compile error

    |             turbonone!(),
    |             ^^^^^^^^^^^^ expected an `Fn<(i32,)>` closure, found `()`
    |
    = help: the trait `Fn<(i32,)>` is not implemented for `()`

After some googling I've figured a way to solve this manually:

foo(None::<fn(_) -> _>)

I'm not sure if there is a way to integrate this solution into the crate though.

Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.