Code Monkey home page Code Monkey logo

fnmutant's Introduction

This (experimental) crate declares a single type FnMutant,

FnMutant has a field f: for<'a> Fn(X) -> Y, which acts much like a fn(X) -> Y function pointer, but is a Trait rather than a type. As such it can take generic parameters.

This specifies that the Fn must be valid for all lifetimes, even those less than the lifetime of f. any references given to f, through X, cannot not be captured by f.

This results in a function that can be called multiple times (unlike FnOnce), and can not capture the unique &mut borrows (unlike FnMut).

To explain why this type is not a simple type alias for for<'a> Fn(x) -> Y, we can add the type alias, FnMutantGoodError below, And the resulting compiler error from some changes in the tests.

pub type FnMutantGoodError<'a, In, Extra, Out, F=&'a dyn for<'b> Fn(In,Extra) -> Out> = FnMutant<In, Extra, Out, F>;

Specifying the default value of F for both helper and fps, works fine, However if you specify it in one place: fn helper(fps: &[FnMutantGoodError<&mut i32, i32, (), &dyn for<'b> Fn(&mut i32, i32)>], ...

And leave as the default, in the caller:

let fps: [FnMutantGoodError<&mut i32, i32, ()>; 3] = ...

You will see a nice compilation error:

--> src/lib.rs:91:20
    |
 91 |             helper(&fps, &mut xs, expect.clone());
    |                    ^^^^ one type is more general than the other
    |
    = note: expected struct `FnMutant<&mut i32, _, _, &dyn for<'r> std::ops::Fn(&'r mut i32, i32)>`
               found struct `FnMutant<&mut i32, _, _, &dyn std::ops::Fn(&mut i32, i32)>`

The reason is that aliases do not check bounds, thus current usage of the type is not pretty, since there is no way to specify default type parameters.

fnmutant's People

Contributors

ratmice avatar

Stargazers

 avatar

Watchers

 avatar  avatar

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.