1

On a nickel web service I would like to execute a function when a response send finishes. I've found the 'on_send' method on this doc, but I can't get it to build.

I get this error:

type mismatch: the type [closure@src/models/plugins/session.rs:78:22: 78:31 x:_] implements the trait std::ops::FnMut<()>, but the trait for<'r> std::ops::FnMut<(&'r mut nickel::Response<'_, D>,)> is required (expected tuple, found ())

and this one

type mismatch: the type [closure@src/models/plugins/session.rs:78:22: 78:31 x:_] implements the trait std::ops::FnOnce<()>, but the trait for<'r> std::ops::FnOnce<(&'r mut nickel::Response<'_, D>,)> is required (expected tuple, found ())

My code sample is

let mut x: usize = 1;
let update = || x += 2 ;
res.on_send(update);

Where res is &mut Response<'mw, D>

Chris Emerson
  • 13,041
  • 3
  • 44
  • 66
plailopo
  • 683
  • 1
  • 7
  • 12
  • Could you include a complete example which can be compiled to reproduce this error? The error does explain the problem, though - it's expecting a function/closure which takes an `&'r mut Response` but you're giving it a closure which doesn't take any arguments. Does `|_| x+=2` work? – Chris Emerson Jan 16 '17 at 12:40
  • The "expected tuple" may be a bit confusing. I'm not sure why it says that, as the expected type is a `Response` which isn't a tuple. Full code example would definitely help, as the errors look a bit odd too. – Peter Hall Jan 16 '17 at 14:10

1 Answers1

2

I'm not familiar with Nickel, but the error is telling you that your closure implements the trait

std::ops::FnOnce<()>

but the argument to on_send needs to be

for<'r> std::ops::FnOnce<(&'r mut nickel::Response<'_, D>,)>

Without a complete code example, it's hard to give you a working answer. But the right answer will be something similar to this:

let update = |_| x += 2;

You aren't using the tuple argument, so you can mark it unused with _. But you must specify it or else the types do not match up.

Peter Hall
  • 53,120
  • 14
  • 139
  • 204