pub struct Span { /* private fields */ }
Expand description
A closed interval of coordinates in one dimension.
Represents the range [start, stop]
.
Implementations§
source§impl Span
impl Span
sourcepub const unsafe fn new_unchecked(start: i64, stop: i64) -> Self
pub const unsafe fn new_unchecked(start: i64, stop: i64) -> Self
sourcepub const fn from_point(x: i64) -> Self
pub const fn from_point(x: i64) -> Self
Creates a span of zero length encompassing the given point.
sourcepub const fn with_start_and_length(start: i64, length: i64) -> Self
pub const fn with_start_and_length(start: i64, length: i64) -> Self
Creates a span of the given length starting from start
.
sourcepub const fn with_stop_and_length(stop: i64, length: i64) -> Self
pub const fn with_stop_and_length(stop: i64, length: i64) -> Self
Creates a span of the given length ending at stop
.
sourcepub const fn with_point_and_length(sign: Sign, point: i64, length: i64) -> Self
pub const fn with_point_and_length(sign: Sign, point: i64, length: i64) -> Self
sourcepub const fn expand(self, sign: Sign, amount: i64) -> Self
pub const fn expand(self, sign: Sign, amount: i64) -> Self
Creates a new Span
expanded by amount
in the direction indicated by pos
.
sourcepub const fn expand_all(self, amount: i64) -> Self
pub const fn expand_all(self, amount: i64) -> Self
Creates a new Span
expanded by amount
in both directions.
sourcepub fn dist_to(&self, point: i64) -> i64
pub fn dist_to(&self, point: i64) -> i64
Gets the shortest distance between this span and a point.
§Example
let span = Span::new(10, 20);
assert_eq!(span.dist_to(4), 6);
assert_eq!(span.dist_to(12), 0);
assert_eq!(span.dist_to(27), 7);
sourcepub fn from_center_span(center: i64, span: i64) -> Self
pub fn from_center_span(center: i64, span: i64) -> Self
Creates a new Span
with center center
and length span
.
span
must be a positive, even integer.
§Example
let span = Span::from_center_span(0, 40);
assert_eq!(span, Span::new(-20, 20));
§Panics
Passing an odd span
to this method results in a panic:
let span = Span::from_center_span(0, 25);
Passing a negative span
to this method also results in a panic:
let span = Span::from_center_span(0, -200);
sourcepub fn from_center_span_gridded(center: i64, span: i64, grid: i64) -> Self
pub fn from_center_span_gridded(center: i64, span: i64, grid: i64) -> Self
Creates a new Span
with center center
and length span
and snap the edges to the
grid.
§Example
let span = Span::from_center_span_gridded(0, 40, 5);
assert_eq!(span, Span::new(-20, 20));
let span = Span::from_center_span_gridded(35, 40, 5);
assert_eq!(span, Span::new(15, 55));
§Panics
This function panics if span
is negative, odd, or not an integer multiple of grid
.
sourcepub const fn intersects(&self, other: &Self) -> bool
pub const fn intersects(&self, other: &Self) -> bool
Checks if the span intersects with the Span
other
.
sourcepub fn merge(spans: impl IntoIterator<Item = Self>) -> Self
pub fn merge(spans: impl IntoIterator<Item = Self>) -> Self
Creates a new minimal Span
that contains all of the elements of spans
.
sourcepub fn union(self, other: Self) -> Self
pub fn union(self, other: Self) -> Self
Calculates the smallest interval containing this span and other
.
sourcepub fn union_all<T>(spans: impl Iterator<Item = T>) -> Selfwhere
T: Into<Self>,
pub fn union_all<T>(spans: impl Iterator<Item = T>) -> Selfwhere
T: Into<Self>,
Calculates the minimal bounding interval of all spans provided.
§Example
let spans = vec![
Span::new(10, 40),
Span::new(35, 60),
Span::new(20, 30),
Span::new(-10, 5),
];
assert_eq!(Span::union_all(spans.into_iter()), Span::new(-10, 60));
§Panics
This function panics if the provided iterator has no elements.
If your iterator may be empty, consider using Span::union_all_option
.
sourcepub fn union_all_option<T>(spans: impl Iterator<Item = T>) -> Option<Self>
pub fn union_all_option<T>(spans: impl Iterator<Item = T>) -> Option<Self>
Calculates the minimal bounding interval of all Option<Span>
s provided.
All None
elements in the iterator are ignored.
If the iterator has no Some(_)
elements, this function returns None
.
§Example
let spans = vec![
Some(Span::new(10, 40)),
Some(Span::new(35, 60)),
None,
Some(Span::new(20, 30)),
None,
Some(Span::new(-10, 5)),
];
assert_eq!(Span::union_all_option(spans.into_iter()), Some(Span::new(-10, 60)));
sourcepub fn intersection(self, other: Self) -> Option<Self>
pub fn intersection(self, other: Self) -> Option<Self>
Calculates the intersection of this span with other
.
sourcepub fn add_point(self, pos: i64) -> Self
pub fn add_point(self, pos: i64) -> Self
Returns a new Span
representing the union of the current span with the given point.
sourcepub const fn shrink_all(self, amount: i64) -> Self
pub const fn shrink_all(self, amount: i64) -> Self
Shrinks the span by the given amount on all sides.
sourcepub fn dist_to_span(self, other: Span) -> i64
pub fn dist_to_span(self, other: Span) -> i64
The minimum separation between this span and other
.
§Example
let s1 = Span::new(10, 20);
let s2 = Span::new(30, 50);
let s3 = Span::new(25, 40);
assert_eq!(s1.dist_to_span(s2), 10);
assert_eq!(s1.dist_to_span(s3), 5);
assert_eq!(s2.dist_to_span(s3), 0);
assert_eq!(s3.dist_to_span(s3), 0);
sourcepub fn has_integer_center(&self) -> bool
pub fn has_integer_center(&self) -> bool
Returns whether the span’s center is at an integer coordinate.
§Example
let span = Span::new(0, 100);
assert!(span.has_integer_center());
let span = Span::new(0, 99);
assert!(!span.has_integer_center());
Trait Implementations§
source§impl BoundingUnion<Span> for Span
impl BoundingUnion<Span> for Span
source§impl Contains<Span> for Span
impl Contains<Span> for Span
source§fn contains(&self, other: &Span) -> Containment
fn contains(&self, other: &Span) -> Containment
source§fn partially_intersects(&self, other: &T) -> bool
fn partially_intersects(&self, other: &T) -> bool
other
is fully or partially enclosed in this shape.source§impl<'de> Deserialize<'de> for Span
impl<'de> Deserialize<'de> for Span
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
source§impl Ord for Span
impl Ord for Span
source§impl PartialEq for Span
impl PartialEq for Span
source§impl PartialOrd for Span
impl PartialOrd for Span
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moreimpl Copy for Span
impl Eq for Span
impl StructuralPartialEq for Span
Auto Trait Implementations§
impl RefUnwindSafe for Span
impl Send for Span
impl Sync for Span
impl Unpin for Span
impl UnwindSafe for Span
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> BoundingUnion<Option<T>> for Twhere
T: BoundingUnion<T, Output = T> + Clone,
impl<T> BoundingUnion<Option<T>> for Twhere
T: BoundingUnion<T, Output = T> + Clone,
source§fn bounding_union(
&self,
other: &Option<T>
) -> <T as BoundingUnion<Option<T>>>::Output
fn bounding_union( &self, other: &Option<T> ) -> <T as BoundingUnion<Option<T>>>::Output
other
. Read more