Advertisement
Guest User

Untitled

a guest
Mar 7th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. use std::borrow::Cow;
  2.  
  3. pub struct SmbClient<'a> {
  4. //ctx: *mut SMBCCTX,
  5. #[allow(dead_code)]
  6. auth_fn: &'a for<'b> Fn(&'b str, &'b str) -> (Cow<'a, str>, Cow<'a, str>, Cow<'a, str>),
  7. }
  8.  
  9. impl<'a> SmbClient<'a> {
  10. pub fn new<F>(auth_fn: &'a F) -> Option<SmbClient<'a>>
  11. where F: for<'b> Fn(&'b str, &'b str) -> (Cow<'a, str>, Cow<'a, str>, Cow<'a, str>) {
  12. let smbc = SmbClient {
  13. //ctx: ptr::null_mut(),
  14. auth_fn: auth_fn,
  15. };
  16. Some(smbc)
  17. }
  18. }
  19.  
  20. //type AuthType<'a> = for<'b> Fn(&'b str, &'b str) -> (Cow<'a, str>, Cow<'a, str>, Cow<'a, str>) + 'a;
  21.  
  22. pub struct Builder<'a,F:for<'b> Fn(&'b str, &'b str) -> (Cow<'a, str>, Cow<'a, str>, Cow<'a, str>)> {
  23. _auth:F,
  24. auth_ref: &'a F,
  25. }
  26.  
  27. pub fn new_builder<'a>(username: &'a str, password: &'a str, domain: &'a str) ->
  28. Builder<'a,impl for<'b> Fn(&'b str, &'b str) -> (Cow<'a, str>, Cow<'a, str>, Cow<'a, str>)>
  29. {
  30. let (auth,auth_ref) = unsafe{
  31. let func=move |_host: &str, _share: &str| {
  32. (Cow::from(domain), Cow::from(username), Cow::from(password))
  33. };
  34. let func_ref= {
  35. &*(&func as *const _)
  36. };
  37. (func,func_ref)
  38. };
  39. Builder {
  40. _auth:auth,
  41. auth_ref,
  42. }
  43. }
  44.  
  45. impl <'a,F:for<'b> Fn(&'b str, &'b str) -> (Cow<'a, str>, Cow<'a, str>, Cow<'a, str>)>
  46. Builder<'a,F>
  47. {
  48. fn build(&self) -> Connector<'a>{
  49. let client=SmbClient::new(self.auth_ref);
  50. Connector{
  51. client
  52. }
  53. }
  54. }
  55.  
  56. struct Connector<'x>
  57. {
  58. client: Option<SmbClient<'x>>,
  59. }
  60.  
  61. fn main()
  62. {
  63. let builder=new_builder("a","b","c");
  64. let c=builder.build();
  65. println!("Good");
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement