Unnamed repository; edit this file 'description' to name the repository.
Scale back the exposed API surface
| -rw-r--r-- | lib/text-size/Cargo.toml | 3 | ||||
| -rw-r--r-- | lib/text-size/src/lib.rs | 3 | ||||
| -rw-r--r-- | lib/text-size/src/range.rs | 207 | ||||
| -rw-r--r-- | lib/text-size/src/size.rs | 125 | ||||
| -rw-r--r-- | lib/text-size/src/traits.rs | 22 | ||||
| -rw-r--r-- | lib/text-size/tests/main.rs | 78 | ||||
| -rw-r--r-- | lib/text-size/tests/serde.rs | 30 |
7 files changed, 133 insertions, 335 deletions
diff --git a/lib/text-size/Cargo.toml b/lib/text-size/Cargo.toml index b5d42f0182..95c11472cd 100644 --- a/lib/text-size/Cargo.toml +++ b/lib/text-size/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "text-size" -version = "0.99.0-dev.1" +version = "0.99.0-dev.2" edition = "2018" authors = [ @@ -14,7 +14,6 @@ documentation = "https://docs.rs/text_unit" [dependencies] serde = { version = "1.0", optional = true, default_features = false } -deepsize = { version = "0.1", optional = true, default_features = false } [dev-dependencies] serde_test = "1.0" diff --git a/lib/text-size/src/lib.rs b/lib/text-size/src/lib.rs index 66bc653790..dc1a09b22b 100644 --- a/lib/text-size/src/lib.rs +++ b/lib/text-size/src/lib.rs @@ -11,6 +11,3 @@ mod traits; mod serde_impls; pub use crate::{range::TextRange, size::TextSize, traits::TextSized}; - -#[cfg(feature = "deepsize")] -deepsize::known_deep_size!(0, TextSize, TextRange); diff --git a/lib/text-size/src/range.rs b/lib/text-size/src/range.rs index 89012f2779..fe227cdaff 100644 --- a/lib/text-size/src/range.rs +++ b/lib/text-size/src/range.rs @@ -1,14 +1,10 @@ use { - crate::{TextSize, TextSized}, + crate::TextSize, std::{ cmp, convert::{TryFrom, TryInto}, fmt, - num::TryFromIntError, - ops::{ - Add, AddAssign, Bound, Index, IndexMut, Range, RangeBounds, RangeInclusive, RangeTo, - RangeToInclusive, Sub, SubAssign, - }, + ops::{Bound, Index, IndexMut, Range, RangeBounds}, }, }; @@ -19,17 +15,17 @@ use { /// /// # Translation from `text_unit` /// -/// - `TextRange::from_to(from, to)` ⟹ `TextRange::from(from..to)` -/// - `TextRange::offset_len(offset, size)` ⟹ `TextRange::at(offset).with_len(size)` -/// - `range.start()` ⟹ `range.start()` -/// - `range.end()` ⟹ `range.end()` -/// - `range.len()` ⟹ `range.len()`<sup>†</sup> -/// - `range.is_empty()` ⟹ `range.is_empty()` -/// - `a.is_subrange(b)` ⟹ `b.contains(a)` -/// - `a.intersection(b)` ⟹ `TextRange::intersection(a, b)` -/// - `a.extend_to(b)` ⟹ `TextRange::covering(a, b)` -/// - `range.contains(offset)` ⟹ `range.contains_point(point)` -/// - `range.contains_inclusive(offset)` ⟹ `range.contains_point_inclusive(point)` +/// - `TextRange::from_to(from, to)` ⟹ `TextRange::from(from..to)` +/// - `TextRange::offset_len(offset, size)` ⟹ `TextRange::from(offset..offset + size)` +/// - `range.start()` ⟹ `range.start()` +/// - `range.end()` ⟹ `range.end()` +/// - `range.len()` ⟹ `range.len()`<sup>†</sup> +/// - `range.is_empty()` ⟹ `range.is_empty()` +/// - `a.is_subrange(b)` ⟹ `b.contains(a)` +/// - `a.intersection(b)` ⟹ `TextRange::intersection(a, b)` +/// - `a.extend_to(b)` ⟹ `TextRange::covering(a, b)` +/// - `range.contains(offset)` ⟹ `range.contains_exclusive(point)` +/// - `range.contains_inclusive(offset)` ⟹ `range.contains_inclusive(point)` /// /// † See the note on [`TextRange::len`] for differing behavior for incorrect reverse ranges. #[derive(Copy, Clone, Eq, PartialEq, Hash)] @@ -45,12 +41,6 @@ pub(crate) const fn TextRange(start: TextSize, end: TextSize) -> TextRange { impl fmt::Debug for TextRange { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - fmt::Display::fmt(self, f) - } -} - -impl fmt::Display for TextRange { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "[{}..{})", self.start(), self.end()) } } @@ -91,27 +81,6 @@ impl TextRange { /// Manipulation methods. impl TextRange { - /// A range covering the text size of some text-like object. - pub fn of(size: impl TextSized) -> TextRange { - TextRange(0.into(), size.text_size()) - } - - /// An empty range at some text size offset. - pub fn at(size: impl Into<TextSize>) -> TextRange { - let size = size.into(); - TextRange(size, size) - } - - /// Set the length without changing the starting offset. - pub fn with_len(self, len: impl Into<TextSize>) -> TextRange { - TextRange(self.start(), self.start() + len.into()) - } - - /// Set the starting offset without changing the length. - pub fn with_offset(self, offset: impl Into<TextSize>) -> TextRange { - TextRange::at(offset).with_len(self.len()) - } - /// Check if this range completely contains another range. pub fn contains(self, other: TextRange) -> bool { self.start() <= other.start() && other.end() <= self.end() @@ -135,7 +104,7 @@ impl TextRange { /// Check if this range contains a point. /// /// The end index is considered excluded. - pub fn contains_point(self, point: impl Into<TextSize>) -> bool { + pub fn contains_exclusive(self, point: impl Into<TextSize>) -> bool { let point = point.into(); self.start() <= point && point < self.end() } @@ -143,40 +112,27 @@ impl TextRange { /// Check if this range contains a point. /// /// The end index is considered included. - pub fn contains_point_inclusive(self, point: impl Into<TextSize>) -> bool { + pub fn contains_inclusive(self, point: impl Into<TextSize>) -> bool { let point = point.into(); self.start() <= point && point <= self.end() } +} - /// Offset the entire range by some text size. - pub fn checked_add(self, rhs: impl TryInto<TextSize>) -> Option<TextRange> { - let rhs = rhs.try_into().ok()?; - Some(TextRange( - self.start().checked_add(rhs)?, - self.end().checked_add(rhs)?, - )) - } - - /// Offset the entire range by some text size. - pub fn checked_sub(self, rhs: impl TryInto<TextSize>) -> Option<TextRange> { - let rhs = rhs.try_into().ok()?; - Some(TextRange( - self.start().checked_sub(rhs)?, - self.end().checked_sub(rhs)?, - )) - } +fn ix(size: TextSize) -> usize { + size.try_into() + .unwrap_or_else(|_| panic!("overflow when converting TextSize to usize index")) } impl Index<TextRange> for str { type Output = str; fn index(&self, index: TextRange) -> &Self::Output { - &self[index.start().ix()..index.end().ix()] + &self[ix(index.start())..ix(index.end())] } } impl IndexMut<TextRange> for str { fn index_mut(&mut self, index: TextRange) -> &mut Self::Output { - &mut self[index.start().ix()..index.end().ix()] + &mut self[ix(index.start())..ix(index.end())] } } @@ -197,29 +153,7 @@ macro_rules! conversions { TextRange(value.start.into(), value.end.into()) } } - impl TryFrom<RangeInclusive<$lte>> for TextRange { - type Error = TryFromIntError; - fn try_from(value: RangeInclusive<$lte>) -> Result<TextRange, Self::Error> { - let (start, end) = value.into_inner(); - let end: TextSize = end.into(); - // This is the only way to get a TryFromIntError currently. - let end = end.checked_add(1).ok_or_else(|| u8::try_from(-1).unwrap_err())?; - Ok(TextRange(start.into(), end)) - } - } - impl From<RangeTo<$lte>> for TextRange { - fn from(value: RangeTo<$lte>) -> TextRange { - TextRange(0.into(), value.end.into()) - } - } - impl TryFrom<RangeToInclusive<$lte>> for TextRange { - type Error = TryFromIntError; - fn try_from(value: RangeToInclusive<$lte>) -> Result<TextRange, Self::Error> { - let start: TextSize = 0.into(); - let end: TextSize = value.end.into(); - TextRange::try_from(start..=end) - } - } + // Just support `start..end` for now, not `..end`, `start..=end`, `..=end`. }; (TryFrom<$gt:ident> for TextRange) => { impl TryFrom<Range<$gt>> for TextRange { @@ -228,30 +162,7 @@ macro_rules! conversions { Ok(TextRange(value.start.try_into()?, value.end.try_into()?)) } } - impl TryFrom<RangeInclusive<$gt>> for TextRange { - type Error = TryFromIntError; - fn try_from(value: RangeInclusive<$gt>) -> Result<TextRange, Self::Error> { - let (start, end) = value.into_inner(); - let end: TextSize = end.try_into()?; - // This is the only way to get a TryFromIntError currently. - let end = end.checked_add(1).ok_or_else(|| u8::try_from(-1).unwrap_err())?; - Ok(TextRange(start.try_into()?, end)) - } - } - impl TryFrom<RangeTo<$gt>> for TextRange { - type Error = TryFromIntError; - fn try_from(value: RangeTo<$gt>) -> Result<TextRange, Self::Error> { - Ok(TextRange(0.into(), value.end.try_into()?)) - } - } - impl TryFrom<RangeToInclusive<$gt>> for TextRange { - type Error = TryFromIntError; - fn try_from(value: RangeToInclusive<$gt>) -> Result<TextRange, Self::Error> { - let start: TextSize = 0.into(); - let end: TextSize = value.end.try_into()?; - TextRange::try_from(start..=end) - } - } + // Just support `start..end` for now, not `..end`, `start..=end`, `..=end`. }; { lt TextSize [$($lt:ident)*] @@ -260,11 +171,8 @@ macro_rules! conversions { varries [$($var:ident)*] } => { $( - // Not `From` yet because of integer type fallback. We want e.g. - // `TextRange::from(0)` and `range + 1` to work, and more `From` - // impls means that this will try (and fail) to use i32 rather - // than one of the unsigned integer types that actually work. - conversions!(TryFrom<$lt> for TextRange); + conversions!(From<$lt> for TextRange); + // unlike TextSize, we do not provide conversions in the "out" direction. )* $( @@ -288,68 +196,3 @@ conversions! { gt TextSize [u64] varries [usize] } - -impl Into<TextRange> for &'_ TextRange { - fn into(self) -> TextRange { - *self - } -} - -impl Into<TextRange> for &'_ mut TextRange { - fn into(self) -> TextRange { - *self - } -} - -macro_rules! op { - (impl $Op:ident for TextRange by fn $f:ident = $op:tt) => { - impl<IntoSize: Copy> $Op<IntoSize> for TextRange - where - TextSize: $Op<IntoSize, Output = TextSize>, - { - type Output = TextRange; - fn $f(self, rhs: IntoSize) -> TextRange { - TextRange(self.start() $op rhs, self.end() $op rhs) - } - } - impl<IntoSize> $Op<IntoSize> for &'_ TextRange - where - TextRange: $Op<IntoSize, Output = TextRange>, - { - type Output = TextRange; - fn $f(self, rhs: IntoSize) -> TextRange { - *self $op rhs - } - } - impl<IntoSize> $Op<IntoSize> for &'_ mut TextRange - where - TextRange: $Op<IntoSize, Output = TextRange>, - { - type Output = TextRange; - fn $f(self, rhs: IntoSize) -> TextRange { - *self $op rhs - } - } - }; -} - -op!(impl Add for TextRange by fn add = +); -op!(impl Sub for TextRange by fn sub = -); - -impl<A> AddAssign<A> for TextRange -where - TextRange: Add<A, Output = TextRange>, -{ - fn add_assign(&mut self, rhs: A) { - *self = *self + rhs - } -} - -impl<S> SubAssign<S> for TextRange -where - TextRange: Sub<S, Output = TextRange>, -{ - fn sub_assign(&mut self, rhs: S) { - *self = *self - rhs - } -} diff --git a/lib/text-size/src/size.rs b/lib/text-size/src/size.rs index 80dba0aba6..43bf19dac9 100644 --- a/lib/text-size/src/size.rs +++ b/lib/text-size/src/size.rs @@ -16,10 +16,10 @@ use { /// /// # Translation from `text_unit` /// -/// - `TextUnit::of_char(c)` ⟹ `TextSize::of(c)` -/// - `TextUnit::of_str(s)` ⟹ `TextSize:of(s)` -/// - `TextUnit::from_usize(size)` ⟹ `TextSize::new(size)` -/// - `unit.to_usize()` ⟹ `size.ix()` +/// - `TextUnit::of_char(c)` ⟹ `TextSize::of(c)` +/// - `TextUnit::of_str(s)` ⟹ `TextSize:of(s)` +/// - `TextUnit::from_usize(size)` ⟹ `TextSize::try_from(size).unwrap_or_else(|| panic!(_))` +/// - `unit.to_usize()` ⟹ `usize::try_from(size).unwrap_or_else(|| panic!(_))` #[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct TextSize { pub(crate) raw: u32, @@ -44,38 +44,16 @@ impl fmt::Display for TextSize { impl TextSize { /// The text size of some text-like object. - pub fn of(text: &impl TextSized) -> TextSize { + pub fn of(text: impl TextSized) -> TextSize { text.text_size() } - /// A text size for some `usize`. + /// A size of zero. /// - /// # Panics - /// - /// Panics if the size is greater than `u32::MAX` and debug assertions are - /// enabled. If debug assertions are not enabled, wraps into `u32` space. - pub fn new(size: usize) -> TextSize { - if let Ok(size) = size.try_into() { - size - } else if cfg!(debug_assertions) { - panic!("overflow when converting to TextSize"); - } else { - TextSize(size as u32) - } - } - - /// Convert this text size into the standard indexing type. - /// - /// # Panics - /// - /// Panics if the size is greater than `usize::MAX`. This can only - /// occur on targets where `size_of::<usize>() < size_of::<u32>()`. - pub fn ix(self) -> usize { - if let Ok(ix) = self.try_into() { - ix - } else { - panic!("overflow when converting TextSize to usize index") - } + /// This is equivalent to `TextSize::default()` or [`TextSize::MIN`], + /// but is more explicit on intent. + pub const fn zero() -> TextSize { + TextSize(0) } } @@ -88,14 +66,12 @@ impl TextSize { pub const MAX: TextSize = TextSize(u32::MAX); #[allow(missing_docs)] - pub fn checked_add(self, rhs: impl TryInto<TextSize>) -> Option<TextSize> { - let rhs = rhs.try_into().ok()?; + pub fn checked_add(self, rhs: TextSize) -> Option<TextSize> { self.raw.checked_add(rhs.raw).map(TextSize) } #[allow(missing_docs)] - pub fn checked_sub(self, rhs: impl TryInto<TextSize>) -> Option<TextSize> { - let rhs = rhs.try_into().ok()?; + pub fn checked_sub(self, rhs: TextSize) -> Option<TextSize> { self.raw.checked_sub(rhs.raw).map(TextSize) } } @@ -138,11 +114,7 @@ macro_rules! conversions { varries [$($var:ident)*] } => { $( - // Not `From` yet because of integer type fallback. We want e.g. - // `TextSize::from(0)` and `size + 1` to work, and more `From` - // impls means that this will try (and fail) to use i32 rather - // than one of the unsigned integer types that actually work. - conversions!(TryFrom<$lt> for TextSize); + conversions!(From<$lt> for TextSize); conversions!(TryFrom<TextSize> for $lt); )* @@ -167,54 +139,17 @@ conversions! { lt u32 [u8 u16] eq u32 [u32] gt u32 [u64] - varries [usize i32] // i32 so that `checked_add($lit)` (`try_from($lit)`) can work - // this will unfortunately have to hang around even if integer literal type fallback improves + varries [usize] } -impl Into<TextSize> for &'_ TextSize { - fn into(self) -> TextSize { - *self +// NB: We do not provide the transparent-ref impls like the stdlib does. +impl Add for TextSize { + type Output = TextSize; + fn add(self, rhs: TextSize) -> TextSize { + TextSize(self.raw + rhs.raw) } } -impl Into<TextSize> for &'_ mut TextSize { - fn into(self) -> TextSize { - *self - } -} - -macro_rules! op { - (impl $Op:ident for TextSize by fn $f:ident = $op:tt) => { - impl<IntoSize: Into<TextSize>> $Op<IntoSize> for TextSize { - type Output = TextSize; - fn $f(self, rhs: IntoSize) -> TextSize { - TextSize(self.raw $op rhs.into().raw) - } - } - impl<IntoSize> $Op<IntoSize> for &'_ TextSize - where - TextSize: $Op<IntoSize, Output = TextSize>, - { - type Output = TextSize; - fn $f(self, rhs: IntoSize) -> TextSize { - *self $op rhs - } - } - impl<IntoSize> $Op<IntoSize> for &'_ mut TextSize - where - TextSize: $Op<IntoSize, Output = TextSize>, - { - type Output = TextSize; - fn $f(self, rhs: IntoSize) -> TextSize { - *self $op rhs - } - } - }; -} - -op!(impl Add for TextSize by fn add = +); -op!(impl Sub for TextSize by fn sub = -); - impl<A> AddAssign<A> for TextSize where TextSize: Add<A, Output = TextSize>, @@ -224,6 +159,13 @@ where } } +impl Sub for TextSize { + type Output = TextSize; + fn sub(self, rhs: TextSize) -> TextSize { + TextSize(self.raw - rhs.raw) + } +} + impl<S> SubAssign<S> for TextSize where TextSize: Sub<S, Output = TextSize>, @@ -233,14 +175,11 @@ where } } -impl iter::Sum for TextSize { - fn sum<I: Iterator<Item = TextSize>>(iter: I) -> TextSize { - iter.fold(TextSize::default(), Add::add) - } -} - -impl<'a> iter::Sum<&'a Self> for TextSize { - fn sum<I: Iterator<Item = &'a Self>>(iter: I) -> Self { - iter.fold(TextSize::default(), Add::add) +impl<A> iter::Sum<A> for TextSize +where + TextSize: Add<A, Output = TextSize>, +{ + fn sum<I: Iterator<Item = A>>(iter: I) -> TextSize { + iter.fold(TextSize::zero(), Add::add) } } diff --git a/lib/text-size/src/traits.rs b/lib/text-size/src/traits.rs index 52601534d2..877f057895 100644 --- a/lib/text-size/src/traits.rs +++ b/lib/text-size/src/traits.rs @@ -4,26 +4,32 @@ use { }; /// Text-like structures that have a text size. -pub trait TextSized { +pub trait TextSized: Copy { /// The size of this text-alike. - fn text_size(&self) -> TextSize; + fn text_size(self) -> TextSize; } -impl TextSized for str { - fn text_size(&self) -> TextSize { +impl TextSized for &'_ str { + fn text_size(self) -> TextSize { let len = self.len(); - TextSize::new(len) + if let Ok(size) = len.try_into() { + size + } else if cfg!(debug_assertions) { + panic!("overflow when converting to TextSize"); + } else { + TextSize(len as u32) + } } } impl TextSized for char { - fn text_size(&self) -> TextSize { - self.len_utf8().try_into().unwrap() + fn text_size(self) -> TextSize { + TextSize(self.len_utf8() as u32) } } impl TextSized for TextRange { - fn text_size(&self) -> TextSize { + fn text_size(self) -> TextSize { self.len() } } diff --git a/lib/text-size/tests/main.rs b/lib/text-size/tests/main.rs index a7eef0a2cd..3288c27320 100644 --- a/lib/text-size/tests/main.rs +++ b/lib/text-size/tests/main.rs @@ -1,67 +1,73 @@ -use text_size::*; +use {std::ops, text_size::*}; -fn r(from: u32, to: u32) -> TextRange { - TextRange::from(from..to) +fn size(x: u32) -> TextSize { + TextSize::from(x) +} + +fn range(x: ops::Range<u32>) -> TextRange { + TextRange::from(x) } #[test] fn sum() { - let xs: Vec<TextSize> = vec![0.into(), 1.into(), 2.into()]; - assert_eq!(xs.iter().sum::<TextSize>(), 3.into()); - assert_eq!(xs.into_iter().sum::<TextSize>(), 3.into()); + let xs: Vec<TextSize> = vec![size(0), size(1), size(2)]; + assert_eq!(xs.iter().copied().sum::<TextSize>(), size(3)); + assert_eq!(xs.into_iter().sum::<TextSize>(), size(3)); } #[test] fn math() { - let range = r(10, 20); - assert_eq!(range + 5, r(15, 25)); - assert_eq!(range - 5, r(5, 15)); + assert_eq!(size(10) + size(5), size(15)); + assert_eq!(size(10) - size(5), size(5)); } #[test] fn checked_math() { - let x: TextSize = 1.into(); - assert_eq!(x.checked_sub(1), Some(0.into())); - assert_eq!(x.checked_sub(2), None); - - assert_eq!(r(1, 2).checked_sub(1), Some(r(0, 1))); - assert_eq!(x.checked_sub(2), None); + assert_eq!(size(1).checked_add(size(1)), Some(size(2))); + assert_eq!(size(1).checked_sub(size(1)), Some(size(0))); + assert_eq!(size(1).checked_sub(size(2)), None); + assert_eq!(TextSize::MAX.checked_add(size(1)), None); } #[test] +#[rustfmt::skip] fn contains() { - let r1 = r(2, 4); - let r2 = r(2, 3); - let r3 = r(1, 3); - assert!(r1.contains(r2)); - assert!(!r1.contains(r3)); + assert!( range(2..4).contains(range(2..3))); + assert!( ! range(2..4).contains(range(1..3))); } #[test] fn intersection() { - assert_eq!(TextRange::intersection(r(1, 2), r(2, 3)), Some(r(2, 2))); - assert_eq!(TextRange::intersection(r(1, 5), r(2, 3)), Some(r(2, 3))); - assert_eq!(TextRange::intersection(r(1, 2), r(3, 4)), None); + assert_eq!( + TextRange::intersection(range(1..2), range(2..3)), + Some(range(2..2)) + ); + assert_eq!( + TextRange::intersection(range(1..5), range(2..3)), + Some(range(2..3)) + ); + assert_eq!(TextRange::intersection(range(1..2), range(3..4)), None); } #[test] fn covering() { - assert_eq!(TextRange::covering(r(1, 2), r(2, 3)), r(1, 3)); - assert_eq!(TextRange::covering(r(1, 5), r(2, 3)), r(1, 5)); - assert_eq!(TextRange::covering(r(1, 2), r(4, 5)), r(1, 5)); + assert_eq!(TextRange::covering(range(1..2), range(2..3)), range(1..3)); + assert_eq!(TextRange::covering(range(1..5), range(2..3)), range(1..5)); + assert_eq!(TextRange::covering(range(1..2), range(4..5)), range(1..5)); } #[test] +#[rustfmt::skip] fn contains_point() { - assert!(!r(1, 3).contains_point(0)); - assert!(r(1, 3).contains_point(1)); - assert!(r(1, 3).contains_point(2)); - assert!(!r(1, 3).contains_point(3)); - assert!(!r(1, 3).contains_point(4)); + assert!( ! range(1..3).contains_exclusive(size(0))); + assert!( range(1..3).contains_exclusive(size(1))); + assert!( range(1..3).contains_exclusive(size(2))); + assert!( ! range(1..3).contains_exclusive(size(3))); + assert!( ! range(1..3).contains_exclusive(size(4))); - assert!(!r(1, 3).contains_point_inclusive(0)); - assert!(r(1, 3).contains_point_inclusive(1)); - assert!(r(1, 3).contains_point_inclusive(2)); - assert!(r(1, 3).contains_point_inclusive(3)); - assert!(!r(1, 3).contains_point_inclusive(4)); + assert!( ! range(1..3).contains_inclusive(size(0))); + assert!( range(1..3).contains_inclusive(size(1))); + assert!( range(1..3).contains_inclusive(size(2))); + assert!( range(1..3).contains_inclusive(size(3))); + assert!( ! range(1..3).contains_inclusive(size(4))); } diff --git a/lib/text-size/tests/serde.rs b/lib/text-size/tests/serde.rs index 439b9d71f5..62254634dd 100644 --- a/lib/text-size/tests/serde.rs +++ b/lib/text-size/tests/serde.rs @@ -1,17 +1,25 @@ -use {serde_test::*, text_size::*}; +use {serde_test::*, std::ops, text_size::*}; + +fn size(x: u32) -> TextSize { + TextSize::from(x) +} + +fn range(x: ops::Range<u32>) -> TextRange { + TextRange::from(x) +} #[test] -fn size() { - assert_tokens(&TextSize::new(00), &[Token::U32(00)]); - assert_tokens(&TextSize::new(10), &[Token::U32(10)]); - assert_tokens(&TextSize::new(20), &[Token::U32(20)]); - assert_tokens(&TextSize::new(30), &[Token::U32(30)]); +fn size_serialization() { + assert_tokens(&size(00), &[Token::U32(00)]); + assert_tokens(&size(10), &[Token::U32(10)]); + assert_tokens(&size(20), &[Token::U32(20)]); + assert_tokens(&size(30), &[Token::U32(30)]); } #[test] -fn range() { +fn range_serialization() { assert_tokens( - &TextRange::from(00..10), + &range(00..10), &[ Token::Tuple { len: 2 }, Token::U32(00), @@ -20,7 +28,7 @@ fn range() { ], ); assert_tokens( - &TextRange::from(10..20), + &range(10..20), &[ Token::Tuple { len: 2 }, Token::U32(10), @@ -29,7 +37,7 @@ fn range() { ], ); assert_tokens( - &TextRange::from(20..30), + &range(20..30), &[ Token::Tuple { len: 2 }, Token::U32(20), @@ -38,7 +46,7 @@ fn range() { ], ); assert_tokens( - &TextRange::from(30..40), + &range(30..40), &[ Token::Tuple { len: 2 }, Token::U32(30), |