Skip to content

Commit

Permalink
Implement From<T> for LocatedSpan
Browse files Browse the repository at this point in the history
  • Loading branch information
kenaniah authored and progval committed May 23, 2020
1 parent 03c25c4 commit edefce7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,12 @@ impl<T: AsBytes, X> LocatedSpan<T, X> {
}
}

impl<T: AsBytes, X: Default> From<T> for LocatedSpan<T, X> {
fn from(i: T) -> Self {
Self::new_extra(i, X::default())
}
}

impl<T: AsBytes + PartialEq, X> PartialEq for LocatedSpan<T, X> {
fn eq(&self, other: &Self) -> bool {
self.line == other.line && self.offset == other.offset && self.fragment == other.fragment
Expand Down
14 changes: 14 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ fn it_should_call_new_for_u8_successfully() {
assert_eq!(BytesSpan::new(input), output);
}

#[test]
fn it_should_convert_from_u8_successfully() {
let input = &b"foobar"[..];
assert_eq!(BytesSpan::new(input), input.into());
assert_eq!(BytesSpanEx::new_extra(input, "extra"), input.into());
}

#[test]
fn it_should_call_new_for_str_successfully() {
let input = &"foobar"[..];
Expand All @@ -64,6 +71,13 @@ fn it_should_call_new_for_str_successfully() {
assert_eq!(StrSpan::new(input), output);
}

#[test]
fn it_should_convert_from_str_successfully() {
let input = &"foobar"[..];
assert_eq!(StrSpan::new(input), input.into());
assert_eq!(StrSpanEx::new_extra(input, "extra"), input.into());
}

#[test]
fn it_should_ignore_extra_for_equality() {
let input = &"foobar"[..];
Expand Down

0 comments on commit edefce7

Please sign in to comment.