-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove the usage of tuples in argument positions from our API #747
Changes from 8 commits
a896743
7df1c22
43f0bfa
6d00e11
b091e8d
edd170d
cec6884
e192fa9
54b9757
fda765c
13992b5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
use std::fmt::{Debug, Formatter, Error as FmtError}; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should probably add some (module-level) docs here. At least for contributors looking to understand diesel's inner workings. Like this maybe. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, the whole thing will need a lot of documentation if merged. |
||
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] | ||
pub struct Cons<Head, Tail>(pub Head, pub Tail); | ||
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] | ||
pub struct Nil; | ||
|
||
pub trait Hlist { | ||
fn len() -> usize; | ||
} | ||
|
||
impl<T, U> Hlist for Cons<T, U> where | ||
U: Hlist, | ||
{ | ||
fn len() -> usize { | ||
U::len() + 1 | ||
} | ||
} | ||
|
||
impl Hlist for Nil { | ||
fn len() -> usize { | ||
0 | ||
} | ||
} | ||
|
||
impl Debug for Nil { | ||
fn fmt(&self, formatter: &mut Formatter) -> Result<(), FmtError> { | ||
formatter.write_str("hlist()") | ||
} | ||
} | ||
|
||
impl<T, U> Debug for Cons<T, U> where | ||
Cons<T, U>: DebugItems, | ||
{ | ||
fn fmt(&self, formatter: &mut Formatter) -> Result<(), FmtError> { | ||
let mut items: &DebugItems = &*self; | ||
let mut formatter = formatter.debug_tuple("hlist"); | ||
while let Some((head, tail)) = items.pop() { | ||
items = tail; | ||
formatter.field(head); | ||
} | ||
formatter.finish() | ||
} | ||
} | ||
|
||
#[doc(hidden)] | ||
pub trait DebugItems { | ||
fn pop(&self) -> Option<(&Debug, &DebugItems)>; | ||
} | ||
|
||
impl<T, U> DebugItems for Cons<T, U> where | ||
T: Debug, | ||
U: DebugItems, | ||
{ | ||
fn pop(&self) -> Option<(&Debug, &DebugItems)> { | ||
Some((&self.0, &self.1)) | ||
} | ||
} | ||
|
||
impl DebugItems for Nil { | ||
fn pop(&self) -> Option<(&Debug, &DebugItems)> { | ||
None | ||
} | ||
} | ||
|
||
#[test] | ||
fn debug_empty_hlist() { | ||
assert_eq!("hlist()", format!("{:?}", Nil)); | ||
assert_eq!("hlist()", format!("{:#?}", Nil)); | ||
} | ||
|
||
#[test] | ||
fn debug_single_item_hlist() { | ||
assert_eq!("hlist(1)", format!("{:?}", Cons(1, Nil))); | ||
assert_eq!("hlist(2)", format!("{:?}", Cons(2, Nil))); | ||
assert_eq!(r#"hlist("hello")"#, format!("{:?}", Cons("hello", Nil))); | ||
assert_eq!(r#"hlist("world")"#, format!("{:?}", Cons("world", Nil))); | ||
} | ||
|
||
#[test] | ||
fn debug_multi_item_hlist() { | ||
assert_eq!("hlist(1, 2)", format!("{:?}", Cons(1, Cons(2, Nil)))); | ||
assert_eq!("hlist(2, 3)", format!("{:?}", Cons(2, Cons(3, Nil)))); | ||
let str_hlist = Cons("hello", Cons("world", Nil)); | ||
assert_eq!(r#"hlist("hello", "world")"#, format!("{:?}", str_hlist)); | ||
let mixed_hlist = Cons("hello", Cons(1, Cons("world", Cons(2, Nil)))); | ||
assert_eq!(r#"hlist("hello", 1, "world", 2)"#, format!("{:?}", mixed_hlist)); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Drive-by comment: Actually, the macro is called
__diesel_column!