Struct geometry::span::Span

source ·
pub struct Span { /* private fields */ }
Expand description

A closed interval of coordinates in one dimension.

Represents the range [start, stop].

Implementations§

source§

impl Span

source

pub const fn until(stop: i64) -> Self

Creates a new Span from 0 until the specified stop.

§Panics

This function panics if stop is less than 0.

source

pub const unsafe fn new_unchecked(start: i64, stop: i64) -> Self

Creates a new Span between two integers.

§Safety

The caller must ensure that start is less than or equal to stop.

source

pub fn new(start: i64, stop: i64) -> Self

Creates a new Span between two integers.

source

pub const fn from_point(x: i64) -> Self

Creates a span of zero length encompassing the given point.

source

pub const fn with_start_and_length(start: i64, length: i64) -> Self

Creates a span of the given length starting from start.

source

pub const fn with_stop_and_length(stop: i64, length: i64) -> Self

Creates a span of the given length ending at stop.

source

pub const fn with_point_and_length(sign: Sign, point: i64, length: i64) -> Self

Creates a span with the given endpoint and length.

If sign is Sign::Pos, point is treated as the ending/stopping point of the span. If sign is Sign::Neg, point is treated as the beginning/starting point of the span.

source

pub const fn expand(self, sign: Sign, amount: i64) -> Self

Creates a new Span expanded by amount in the direction indicated by pos.

source

pub const fn expand_all(self, amount: i64) -> Self

Creates a new Span expanded by amount in both directions.

source

pub const fn endpoint(&self, sign: Sign) -> i64

Gets the starting (Sign::Neg) or stopping (Sign::Pos) endpoint of a span.

source

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);
source

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);
source

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.

source

pub const fn center(&self) -> i64

Gets the center of the span.

source

pub const fn length(&self) -> i64

Gets the length of the span.

source

pub const fn start(&self) -> i64

Gets the start of the span.

source

pub const fn stop(&self) -> i64

Gets the stop of the span.

source

pub const fn intersects(&self, other: &Self) -> bool

Checks if the span intersects with the Span other.

source

pub fn merge(spans: impl IntoIterator<Item = Self>) -> Self

Creates a new minimal Span that contains all of the elements of spans.

source

pub fn union(self, other: Self) -> Self

Calculates the smallest interval containing this span and other.

source

pub fn union_all<T>(spans: impl Iterator<Item = T>) -> Self
where 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.

source

pub fn union_all_option<T>(spans: impl Iterator<Item = T>) -> Option<Self>
where T: Into<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)));
source

pub fn intersection(self, other: Self) -> Option<Self>

Calculates the intersection of this span with other.

source

pub fn add_point(self, pos: i64) -> Self

Returns a new Span representing the union of the current span with the given point.

source

pub fn shrink(self, side: Sign, amount: i64) -> Self

Shrinks the given side by the given amount.

Behavior is controlled by the given Sign:

  • If side is Sign::Pos, shrinks from the positive end (ie. decreases the stop).
  • If side is Sign::Neg, shrinks from the negative end (ie. increases the start).
source

pub const fn shrink_all(self, amount: i64) -> Self

Shrinks the span by the given amount on all sides.

source

pub const fn translate(self, amount: i64) -> Self

Translates the span by the given amount.

source

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);
source

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

§

type Output = Span

The type of the output shape representing the bounding union.
source§

fn bounding_union(&self, other: &Span) -> Self::Output

Calculates the bounding union of this shape with other. Read more
source§

impl Clone for Span

source§

fn clone(&self) -> Span

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Contains<Span> for Span

source§

fn contains(&self, other: &Span) -> Containment

Returns a Containment indicating how other is enclosed within this shape. Read more
source§

fn encloses(&self, other: &T) -> bool

Returns true if other is fully enclosed in this shape.
source§

fn partially_intersects(&self, other: &T) -> bool

Returns true if other is fully or partially enclosed in this shape.
source§

impl Debug for Span

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for Span

source§

fn default() -> Span

Returns the “default value” for a type. Read more
source§

impl<'de> Deserialize<'de> for Span

source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl From<(i64, i64)> for Span

source§

fn from(tup: (i64, i64)) -> Self

Converts to this type from the input type.
source§

impl From<Span> for (i64, i64)

source§

fn from(s: Span) -> Self

Converts to this type from the input type.
source§

impl Hash for Span

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl Intersect<Span> for Span

§

type Output = Span

The type of the output shape representing the intersection.
source§

fn intersect(&self, other: &Span) -> Option<Self::Output>

Calculates the intersection of this shape with other. Read more
source§

impl Ord for Span

source§

fn cmp(&self, other: &Span) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl PartialEq for Span

source§

fn eq(&self, other: &Span) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd for Span

source§

fn partial_cmp(&self, other: &Span) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl Serialize for Span

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl Copy for Span

source§

impl Eq for Span

source§

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> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> BoundingUnion<Option<T>> for T
where T: BoundingUnion<T, Output = T> + Clone,

§

type Output = T

The type of the output shape representing the bounding union.
source§

fn bounding_union( &self, other: &Option<T> ) -> <T as BoundingUnion<Option<T>>>::Output

Calculates the bounding union of this shape with other. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,