Skip to content
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

Add TextRange: TryFrom<Range<T>> impl #53

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "text-size"
version = "1.1.1"
version = "1.2.0"
edition = "2018"

authors = [
Expand Down
16 changes: 16 additions & 0 deletions src/range.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::convert::{TryFrom, TryInto};

use cmp::Ordering;

use {
Expand Down Expand Up @@ -392,6 +394,20 @@ where
}
}

impl<T> TryFrom<Range<T>> for TextRange
where
T: TryInto<TextSize>,
{
type Error = <T as TryInto<TextSize>>::Error;
#[inline]
fn try_from(value: Range<T>) -> Result<Self, Self::Error> {
Ok(TextRange::new(
value.start.try_into()?,
value.end.try_into()?,
))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should check that start <= end?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would require me to rethink the error type and I didn't want to bother with that 😬 I guess discarding the TryInto<TextSize> error and replacing it with a generic one here would be probably fine

}
}

macro_rules! ops {
(impl $Op:ident for TextRange by fn $f:ident = $op:tt) => {
impl $Op<&TextSize> for TextRange {
Expand Down
Loading