Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // lib code
- mod my_generated_crate {
- use std::marker::PhantomData;
- pub struct UserDefinedFunctions<FV, FN> {
- pub version_func: FV,
- pub name_func: FN,
- }
- pub struct MyApi<FV, FN> {
- user_defined_functions: UserDefinedFunctions<FV, FN>,
- }
- impl<FV: Fn() -> i32, FN: Fn() -> &'static str> MyApi<FV, FN> {
- pub fn new(user_defined_functions: UserDefinedFunctions<FV, FN>) -> Self {
- Self {
- user_defined_functions,
- }
- }
- pub fn get_name_and_double_version(&self) -> (i32, &'static str) {
- (
- (self.user_defined_functions.version_func)() * 2,
- (self.user_defined_functions.name_func)(),
- )
- }
- }
- }
- // user code
- use crate::my_generated_crate::{MyApi, UserDefinedFunctions};
- fn version_func() -> i32 {
- 10
- }
- fn name_func() -> &'static str {
- "Hello lib!"
- }
- fn main() {
- let api = MyApi::new(UserDefinedFunctions {
- version_func,
- name_func,
- });
- println!("{:?}", api.get_name_and_double_version());
- let context = 100;
- let api_with_context = MyApi::new(UserDefinedFunctions {
- version_func: || context,
- name_func,
- });
- println!("{:?}", api_with_context.get_name_and_double_version());
- }
Add Comment
Please, Sign In to add comment