SHARE
TWEET

Untitled

a guest Aug 21st, 2019 79 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. use std::marker::PhantomData;
  2. use std::fmt::Error;
  3.  
  4. trait Sample {}
  5. impl Sample for u8 {}
  6. impl Sample for u16 {}
  7. impl Sample for u32 {}
  8.  
  9.  
  10. struct Parameters<S:?Sized> { sample_format: std::marker::PhantomData<S>, }
  11.  
  12.  
  13. // Элиасы(упрощены для наглядности)
  14. pub type InputSource<T> where T : ?Sized + Sample + 'static = Parameters<T>;
  15. pub type OutputSource<T> where T : ?Sized + Sample + 'static = Parameters<T>;
  16.  
  17.  
  18. // Типы флоу, которые . необходимо вернуть и имплементации для них
  19. pub trait AudioFlow<T> where T : ?Sized + Sample + 'static {}
  20. pub struct InputFlow<T> where T : ?Sized + Sample + 'static { pub source: InputSource<T> }
  21. pub struct OutputFlow<T> where T : ?Sized + Sample + 'static { pub source: OutputSource<T> }
  22. уть
  23. impl <T> AudioFlow<T> for InputFlow<T> where T : ?Sized + Sample + 'static {}
  24. impl <T> AudioFlow<T> for OutputFlow<T> where T : ?Sized + Sample + 'static {}
  25.  
  26.  
  27. // Функции создания по типу
  28. pub enum AudioType { Input, Output }
  29. pub fn flow<T>(ty: AudioType) -> Result<Box<dyn AudioFlow<T>>, Error>
  30.     where T : ?Sized + Sample + 'static {
  31.  
  32.     match ty {
  33.         AudioType::Input => flow_input::<T>(),
  34.         AudioType::Output => flow_output::<T>()
  35.     }
  36. }
  37. pub fn flow_input<T>() -> Result<Box<InputFlow<T>>, Error>
  38.     where T : ?Sized + Sample + 'static {
  39.  
  40.     let source = Parameters { sample_format: std::marker::PhantomData };
  41.     return Ok(Box::new(InputFlow { source }))
  42. }
  43. pub fn flow_output<T>() -> Result<Box<OutputFlow<T>>, Error>
  44.     where T : ?Sized + Sample + 'static {
  45.  
  46.     let source = Parameters { sample_format: std::marker::PhantomData };
  47.     return Ok(Box::new(OutputFlow { source }))
  48. }
  49.  
  50.  
  51. // Создание флоу
  52. fn main() {
  53.     let a = flow::<u32>(AudioType::Input);
  54. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top