Guest User

Untitled

a guest
Jan 16th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. // lib code
  2. mod my_generated_crate {
  3. use std::marker::PhantomData;
  4.  
  5. pub struct UserDefinedFunctions<FV, FN> {
  6. pub version_func: FV,
  7. pub name_func: FN,
  8. }
  9.  
  10. pub struct MyApi<FV, FN> {
  11. user_defined_functions: UserDefinedFunctions<FV, FN>,
  12. }
  13.  
  14. impl<FV: Fn() -> i32, FN: Fn() -> &'static str> MyApi<FV, FN> {
  15. pub fn new(user_defined_functions: UserDefinedFunctions<FV, FN>) -> Self {
  16. Self {
  17. user_defined_functions,
  18. }
  19. }
  20.  
  21. pub fn get_name_and_double_version(&self) -> (i32, &'static str) {
  22. (
  23. (self.user_defined_functions.version_func)() * 2,
  24. (self.user_defined_functions.name_func)(),
  25. )
  26. }
  27. }
  28. }
  29.  
  30. // user code
  31. use crate::my_generated_crate::{MyApi, UserDefinedFunctions};
  32.  
  33. fn version_func() -> i32 {
  34. 10
  35. }
  36.  
  37. fn name_func() -> &'static str {
  38. "Hello lib!"
  39. }
  40.  
  41. fn main() {
  42. let api = MyApi::new(UserDefinedFunctions {
  43. version_func,
  44. name_func,
  45. });
  46. println!("{:?}", api.get_name_and_double_version());
  47.  
  48. let context = 100;
  49. let api_with_context = MyApi::new(UserDefinedFunctions {
  50. version_func: || context,
  51. name_func,
  52. });
  53. println!("{:?}", api_with_context.get_name_and_double_version());
  54. }
Add Comment
Please, Sign In to add comment