Guest User

Untitled

a guest
Oct 15th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. #![allow(dead_code)]
  2.  
  3. use std::marker::PhantomData;
  4.  
  5. struct PgValue<'a>(&'a [u8]);
  6. struct Pg;
  7. struct MySql;
  8. struct Int;
  9.  
  10. pub trait FamilyLt<'a> {
  11. type Out;
  12. }
  13.  
  14. struct IdFamily<T>(PhantomData<T>);
  15.  
  16. impl<'a, T> FamilyLt<'a> for IdFamily<T> {
  17. type Out = T;
  18. }
  19.  
  20. struct RefFamily<T: ?Sized>(PhantomData<T>);
  21.  
  22. impl<'a, T: 'a + ?Sized> FamilyLt<'a> for RefFamily<T> {
  23. type Out = &'a T;
  24. }
  25.  
  26. struct PgValueHelper;
  27.  
  28. impl<'a> FamilyLt<'a> for PgValueHelper {
  29. type Out = PgValue<'a>;
  30. }
  31.  
  32. pub trait Backend: Sized {
  33. type RawValue: for<'a> FamilyLt<'a>;
  34. }
  35.  
  36. impl Backend for Pg {
  37. type RawValue = PgValueHelper;
  38. }
  39.  
  40. impl Backend for MySql {
  41. type RawValue = RefFamily<[u8]>;
  42. }
  43.  
  44. pub trait FromSql<A, DB: Backend>: Sized {
  45. fn from_sql<'a>(bytes: Option<<DB::RawValue as FamilyLt<'a>>::Out>) -> Result<Self, ()>;
  46. }
  47.  
  48. impl FromSql<Int, Pg> for i32 {
  49. fn from_sql<'a>(bytes: Option<PgValue<'a>>) -> Result<Self, ()> {
  50. // just something
  51. Ok(bytes.unwrap().0[0] as i32)
  52. }
  53. }
  54.  
  55. pub trait Row<DB: Backend> {
  56. fn take<'a>(&'a mut self) -> Option<<DB::RawValue as FamilyLt<'a>>::Out>;
  57.  
  58. fn next_is_null(&self, count: usize) -> bool;
  59.  
  60. fn advance(&mut self, count: usize) {
  61. for _ in 0..count {
  62. self.take();
  63. }
  64. }
  65. }
  66.  
  67. struct PgResult;
  68.  
  69. impl PgResult {
  70. fn get<'a>(&'a self, _row_idx: usize, _col_idx: usize) -> Option<PgValue<'a>> {
  71. None
  72. }
  73. }
  74.  
  75. struct PgRow<'a> {
  76. db_result: &'a PgResult,
  77. row_idx: usize,
  78. col_idx: usize,
  79. }
  80.  
  81. impl<'a> Row<Pg> for PgRow<'a> {
  82. fn take<'b>(&'b mut self) -> Option<PgValue<'b>> {
  83. self.db_result.get(self.row_idx, self.col_idx)
  84. }
  85.  
  86. fn next_is_null(&self, _count: usize) -> bool {
  87. true
  88. }
  89. }
  90.  
  91. fn main() {
  92. println!("Hello, world!");
  93. }
Add Comment
Please, Sign In to add comment