Extent - replacement for std::ops::{Range,RangeInclusive}

Related tags

Utilities extent
Overview

extent

This is an alternative to std::ops::{Range,RangeInclusive}, avoiding the quirks of those types (non-Copy, inability to produce empty inclusive ranges without extra bool, directly implementing Iterator, etc). See https://ridiculousfish.com/blog/posts/least-favorite-rust-type.html and https://kaylynn.gay/blog/post/rust_ranges_and_suffering for its litany of sins.

I was coincidentally writing this type in one of my own crates today when I saw the second post go by, so I figured I'd split it out and post it as a crate others can use. It has some quirks and limitations of its own but it suits my needs better than the builtins. Here are the choices I made:

  1. Extent represents an inclusive range of numbers, where number is N:PrimInt from the (fairly standard) num-traits crate. It is inclusive because (at least the most obvious representations of) exclusive ranges can't represent the maximum number of a number-type, which in my experience one fairly often needs to represent!

  2. Extent uses exactly 2 numbers and no extra flags or wasted space.

  3. Extent can represent empty ranges. Empty ranges are represented with a normalized form of {lo=1, hi=0}. This is the only case for which lo > hi and is only constructable via the static function empty or the IO-oriented function new_unchecked. Typical accessors for endpoints lo() and hi() return an Option<N> with None in the empty case. If you want the raw form (eg. for doing IO) you can call lo_unchecked() or hi_unchecked(), which are marked unsafe as they do not reflect the significant lo <= hi invariant.

  4. All nonempty cases have lo <= hi enforced in new. If you pass hi > lo to new, the values are swapped (i.e. you can construct from either order of points; they get stored in order). If you are constructing from raw IO values you can do new_unchecked which will not swap, only normalize unordered ranges to empty(), and is also unsafe.

  5. Extent implements Copy (and everything else standard).

  6. Extent does not implement Iterator, but it has an iter method that copies Extent into ExtentIter, which does implement Iterator.

  7. There is also an ExtentRevIter that counts down.

  8. Some basic set-like operators are provided (union, intersection, contains) but nothing too fancy.

Patches are welcome to enrich this further, though I will try to keep it fairly simple and focused on the use-case of number-ranges, not "arbitrary thing ranges".

License: MIT OR Apache-2.0

You might also like...
Zellij is a workspace aimed at developers, ops-oriented people and anyone who loves the terminal
Zellij is a workspace aimed at developers, ops-oriented people and anyone who loves the terminal

Zellij is a workspace aimed at developers, ops-oriented people and anyone who loves the terminal. At its core, it is a terminal multiplexer (similar to tmux and screen), but this is merely its infrastructure layer.

🕵️‍♀️ Find, locate, and query files for ops and security experts ⚡️⚡️⚡️
🕵️‍♀️ Find, locate, and query files for ops and security experts ⚡️⚡️⚡️

Recon Find, locate, and query files for ops and security experts Key Features • How To Use • Download • Contributing • License Key Features Query with

A drop-in replacement for std::time::Instant that is faster and more accurate.

Fastant A drop-in replacement for std::time::Instant that measures time with high performance and high accuracy powered by Time Stamp Counter (TSC). U

Multi-stream HTTP downloader using range requests

chooch - An Amazing Project Downloads files faster than wget/curl (in theory) using multiple connections. Chooch recycles the slowest connection and r

 An iterator following a space-filling pattern over a given range
An iterator following a space-filling pattern over a given range

rlp-iter rlp-iter (Resolving Lattice Point Iterator) is an iterator that returns a space-filling permutation of integers in a given range. Specificall

ZKP fork for rust-secp256k1, adds wrappers for range proofs, pedersen commitments, etc

rust-secp256k1 rust-secp256k1 is a wrapper around libsecp256k1, a C library by Peter Wuille for producing ECDSA signatures using the SECG curve secp25

Bulletproofs and Bulletproofs+ Rust implementation for Aggregated Range Proofs over multiple elliptic curves

Bulletproofs This library implements Bulletproofs+ and Bulletproofs aggregated range proofs with multi-exponent verification. The library supports mul

A range proof SNARK using Spartan

R1CS Gadgets This repository contains R1CS gadgest I have built using (a slightly modified version of) Spartan. Currently, the two gadgets included ar

A Rust crate that implements a range map data structure backed by a Vec.

range_map_vec This crate implements a range map data structure backed by a Vec using binary search. Docs and usage can be found in the corresponding r

Trait that allows comparing a value to a range of values.

range_cmp Docs This Rust crate provides the RangeComparable trait on all types that implement Ord. This traits exposes a rcmp associated method that a

The sysroot manager that lets you build and customize `std`
The sysroot manager that lets you build and customize `std`

PSA: Xargo is in maintenance mode xargo The sysroot manager that lets you build and customize std Cross compiling `std` for i686-unknown-linux-gnu Xar

C++ `std::unique_ptr` that represents each object as an NFT on the Ethereum blockchain
C++ `std::unique_ptr` that represents each object as an NFT on the Ethereum blockchain

C++ `std::unique_ptr` that represents each object as an NFT on the Ethereum blockchain

Totally Speedy Transmute (TST) is a library providing a small, performance oriented, safe version of std::mem::transmute

Totally Speedy Transmute An evil spiritual successor to Totally Safe Transmute What is it? Totally Speedy Transmute (TST) is a library providing a sma

Debug2 is a pretty printing crate based on std::fmt

debug2 is a pretty printing crate based on std::fmt Why not just use Debug The Debug trait is good, but the problem is it is not very good at n

An extension to Rust std for beginner exercises

note: this is for practicing rust only, do not use this in actual production programs! really, don't. [dependencies] simple-std = "0.1.1" simple-std i

argmax is a library that allows Rust applications to avoid Argument list too long errors (E2BIG) by providing a std::process::Command wrapper with a

argmax argmax is a library that allows Rust applications to avoid Argument list too long errors (E2BIG) by providing a std::process::Command wrapper w

single file, std only, async Rust executor

whorl - A single file, std only, async Rust executor whorl was created to teach you how async executors work in Rust. It is not the fastest executor n

std::simd implementation of BLAKE3
std::simd implementation of BLAKE3

BLAKE3-STD the first blake3 implementation on std::simd ONLY COMPILES WITH NIGHTLY [dependencies] blake3-std = "0.0.1" OFFICIAL DOC BLAKE3 is a crypto

A Rust no-std (de)compressor based on PAQ

MASHI まし A 100% no-std compatible Rust implementation of a PAQ style arithmetic coding, context mixing compressor. Its intended use case is compressin

Owner
Graydon Hoare
Graydon Hoare
single file, std only, async Rust executor

whorl - A single file, std only, async Rust executor whorl was created to teach you how async executors work in Rust. It is not the fastest executor n

Michael Gattozzi 459 Dec 29, 2022
dark-std an Implementation of asynchronous containers build on tokio

dark-std dark-std is an Implementation of asynchronous containers build on tokio. It uses a read-write separation design borrowed from Golang SyncHash

darkrpc 4 Dec 13, 2022
no-std no-alloc Rust protobufs.

noproto No-std, no-alloc protocol buffers (protobuf) implementation in Rust, for embedded systems. Optimized for binary size and memory usage, not for

null 13 Jun 21, 2023
A box full of utilities, a unworthy replacement for coreutils / busybox / toybox.

Gearbox A box full of utilities, a unworthy replacement for coreutils / busybox / toybox. List of content How to setup Systems Ubuntu Arch How to buil

Gemlock 5 Jul 21, 2022
A flexible, simple to use, immutable, clone-efficient String replacement for Rust

A flexible, simple to use, immutable, clone-efficient String replacement for Rust. It unifies literals, inlined, and heap allocated strings into a single type.

Scott Meeuwsen 119 Dec 12, 2022
messloc is a drop in replacement for malloc that can transparently recover from memory fragmentation without any changes to application code.

messloc is a drop in replacement for malloc that can transparently recover from memory fragmentation without any changes to application code. Goals Al

null 11 Dec 10, 2022
A mostly drop-in replacement for mercantile written w/ rust, plus several other util(e)ities.

utiles utiles = utils + tiles A mostly drop-in replacement for mercantile written w/ rust, plus several other util(e)ities. Installation pip install u

jesse 5 Jun 20, 2023
Easy c̵̰͠r̵̛̠ö̴̪s̶̩̒s̵̭̀-t̶̲͝h̶̯̚r̵̺͐e̷̖̽ḁ̴̍d̶̖̔ ȓ̵͙ė̶͎ḟ̴͙e̸̖͛r̶̖͗ë̶̱́ṉ̵̒ĉ̷̥e̷͚̍ s̷̹͌h̷̲̉a̵̭͋r̷̫̊ḭ̵̊n̷̬͂g̵̦̃ f̶̻̊ơ̵̜ṟ̸̈́ R̵̞̋ù̵̺s̷̖̅ţ̸͗!̸̼͋

Rust S̵̓i̸̓n̵̉ I̴n̴f̶e̸r̵n̷a̴l mutability! Howdy, friendly Rust developer! Ever had a value get m̵̯̅ð̶͊v̴̮̾ê̴̼͘d away right under your nose just when

null 294 Dec 23, 2022
Expanding opportunities standard library std::fs and std::io

fs_extra A Rust library that provides additional functionality not present in std::fs. Documentation Migrations to 1.x.x version Key features: Copy fi

Denis Kurilenko 163 Dec 30, 2022
Rust on ESP32 STD "Hello, World" app. A "Hello, world!" STD binary crate for the ESP32[XX] and ESP-IDF.

Rust on ESP32 STD "Hello, World" app A "Hello, world!" STD binary crate for the ESP32[XX] and ESP-IDF. This is the crate you get when running cargo ne

Ivan Markov 138 Jan 1, 2023