31: Use standard generics for TextSize::of r=CAD97 a=CAD97
There is no specific reason to use APIT here, so prefer the form that allows more control for the user, in the form of the turbofish.
Co-authored-by: CAD97 <cad97@cad97.com>
30: Don't use decimal numbers when str length overflows TextSize. r=matklad a=CAD97
Since the string is necessarily longer than 4,294,967,296 bytes,
it is probably more useful to output the length in hexadecimal.
For additional niceness, we output the first and last 10 characters.
Co-authored-by: CAD97 <cad97@cad97.com>
29: TextSized is not meant to be used directly... r=matklad a=CAD97
so rename it to a name more distinct from TextSize.
Co-authored-by: CAD97 <cad97@cad97.com>
28: Add doctests to things r=matklad a=CAD97
This also includes a test to make sure the types implement the common std traits.
Co-authored-by: CAD97 <cad97@cad97.com>
23: Add a deref-removing impl for TextSized r=CAD97 a=CAD97
Notably, given `s: &SmolStr`, `TextSize::of(s)` does not work, where `TextUnit::of_str(s)` did, because the concrete type could be deref-coerced to. (It's probably a limitation of the language that deref coersion does not apply here, but it's one we have to live with.)
So we provide a blanket impl for any type that derefs (directly) to `str`.
Alternatively, we can provide a deeper blanket impl that derefs any number of references recursively [[playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=008b5e0c2edd332b34b3efa42ce2a40a)]:
```rust
impl<D> TextSized for &'_ D
where
D: Deref,
for<'a> &'a D::Target: TextSized,
{
#[inline]
fn text_size(self) -> TextSize {
self.deref().text_size()
}
}
```
This is "only" at the cost of a little extra impl complexity.
Co-authored-by: CAD97 <cad97@cad97.com>
26: Make TextRange constructors more boring r=matklad a=matklad
Remove `fn TextRange(` as that's slightly unusual and surprising,
which will add up to a lot of confusion over the long run.
Instead add:
* `new` as the biased, canonical way to create range from bounds
* `from_len` as an alternative ctor from starting position and len
* `empty` for empty ranges at a given offset
* `up_to` for ranges at zero offset with given length
* `default` for an empty range at zero
Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
Remove `fn TextRange(` as that's slightly unusual and surprising,
which will add up to a lot of confusion over the long run.
Instead add:
* `new` as the biased, canonical way to create range from bounds
* `from_len` as an alternative ctor from starting position and len
* `empty` for empty ranges at a given offset
* `up_to` for ranges at zero offset with given length
* `default` for an empty range at zero
18: minor improvements everywhere r=matklad a=CAD97
Because none of this was quite specific enough to break into its own PR
- Update translation docs
- Officially `compiler_error!`-forbid `cfg(target_pointer_width = "16")`
- `TextRange::before` (alt options: `TextRange::to`, `TextRange::up_to`, `TextRangeTo`, `TextSize::prefix`)
- `TextRange::after` (alt options: `TextSize::suffix`)
- `TextRange::offset` (alt options: `TextRange::add`, `Add::add`, `TextRange::reanchor`)
- `TextSize::one` (alt options: )
- `TextSize::ONE` (alt options: `TextSize::ASCII`
- `#[inline]` capability for a bunch of things
This PR is mainly just to get opinions on all of these little things, then I'll re-force-push with a commit just with those.
Co-authored-by: CAD97 <cad97@cad97.com>
13: Alternative set of contains function r=CAD97 a=matklad
Motivation:
TextRange is a set, it contains elements (TextSize). For this reason,
for range-range op we use a more verbose `contains_range` name.
In stdlib, there's `HashSet::is_subset`. We used a similar design with
`is_subrage` before, but it was very confusing in practice -- you'll
have to lookup docs for which of lhs and rhs is sub and super set.
Additionally, exclusive semantics is a clear default with better
properties (if you have a partitioning of a range into subranges, only
one of the parts contains any given offset), so it make sense to call
it `contains` and reserve `contains_inclusive` for another op.
Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
8: Provide ref-transparent ops for TextSize r=matklad a=CAD97
Counter-proposal to rust-analyzer/text-size#7. Basically the same set of impls, but set up slightly differently.
Co-authored-by: CAD97 <cad97@cad97.com>