1use crate::fmt;
2use crate::hash::Hash;
3use crate::marker::Destruct;
4#[lang = "RangeFull"]
40#[doc(alias = "..")]
41#[derive(#[automatically_derived]
#[stable(feature = "rust1", since = "1.0.0")]
impl crate::marker::Copy for RangeFull { }Copy, #[automatically_derived]
#[stable(feature = "rust1", since = "1.0.0")]
impl crate::hash::Hash for RangeFull {
#[inline]
fn hash<__H: crate::hash::Hasher>(&self, state: &mut __H) {}
}Hash)]
42#[derive_const(#[automatically_derived]
#[rustc_const_unstable(feature = "derive_const", issue = "118304")]
#[stable(feature = "rust1", since = "1.0.0")]
const impl crate::clone::Clone for RangeFull {
#[inline]
fn clone(&self) -> RangeFull { *self }
}Clone, #[automatically_derived]
#[rustc_const_unstable(feature = "derive_const", issue = "118304")]
#[stable(feature = "rust1", since = "1.0.0")]
const impl crate::default::Default for RangeFull {
#[inline]
fn default() -> RangeFull { RangeFull {} }
}Default, #[automatically_derived]
#[rustc_const_unstable(feature = "derive_const", issue = "118304")]
#[stable(feature = "rust1", since = "1.0.0")]
const impl crate::cmp::Eq for RangeFull {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {}
}Eq, #[automatically_derived]
#[rustc_const_unstable(feature = "derive_const", issue = "118304")]
#[stable(feature = "rust1", since = "1.0.0")]
const impl crate::cmp::PartialEq for RangeFull {
#[inline]
fn eq(&self, other: &RangeFull) -> bool { true }
}PartialEq)]
43#[stable(feature = "rust1", since = "1.0.0")]
44pub struct RangeFull;
45
46#[stable(feature = "rust1", since = "1.0.0")]
47impl fmt::Debug for RangeFull {
48 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
49 fmt.write_fmt(format_args!(".."))write!(fmt, "..")
50 }
51}
52
53#[lang = "Range"]
78#[doc(alias = "..")]
79#[derive(#[automatically_derived]
#[stable(feature = "rust1", since = "1.0.0")]
impl<Idx: crate::cmp::Eq> crate::cmp::Eq for Range<Idx> {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: crate::cmp::AssertParamIsEq<Idx>;
}
}Eq, #[automatically_derived]
#[stable(feature = "rust1", since = "1.0.0")]
impl<Idx: crate::hash::Hash> crate::hash::Hash for Range<Idx> {
#[inline]
fn hash<__H: crate::hash::Hasher>(&self, state: &mut __H) {
crate::hash::Hash::hash(&self.start, state);
crate::hash::Hash::hash(&self.end, state)
}
}Hash)]
80#[derive_const(#[automatically_derived]
#[rustc_const_unstable(feature = "derive_const", issue = "118304")]
#[stable(feature = "rust1", since = "1.0.0")]
const impl<Idx: [const] crate::clone::Clone> crate::clone::Clone for
Range<Idx> {
#[inline]
fn clone(&self) -> Range<Idx> {
Range {
start: crate::clone::Clone::clone(&self.start),
end: crate::clone::Clone::clone(&self.end),
}
}
}Clone, #[automatically_derived]
#[rustc_const_unstable(feature = "derive_const", issue = "118304")]
#[stable(feature = "rust1", since = "1.0.0")]
const impl<Idx: [const] crate::default::Default> crate::default::Default for
Range<Idx> {
#[inline]
fn default() -> Range<Idx> {
Range {
start: crate::default::Default::default(),
end: crate::default::Default::default(),
}
}
}Default, #[automatically_derived]
#[rustc_const_unstable(feature = "derive_const", issue = "118304")]
#[stable(feature = "rust1", since = "1.0.0")]
const impl<Idx: [const] crate::cmp::PartialEq> crate::cmp::PartialEq for
Range<Idx> {
#[inline]
fn eq(&self, other: &Range<Idx>) -> bool {
self.start == other.start && self.end == other.end
}
}PartialEq)] #[stable(feature = "rust1", since = "1.0.0")]
82pub struct Range<Idx> {
83 #[stable(feature = "rust1", since = "1.0.0")]
85 pub start: Idx,
86 #[stable(feature = "rust1", since = "1.0.0")]
88 pub end: Idx,
89}
90
91#[stable(feature = "rust1", since = "1.0.0")]
92impl<Idx: fmt::Debug> fmt::Debug for Range<Idx> {
93 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
94 self.start.fmt(fmt)?;
95 fmt.write_fmt(format_args!(".."))write!(fmt, "..")?;
96 self.end.fmt(fmt)?;
97 Ok(())
98 }
99}
100
101impl<Idx: PartialOrd<Idx>> Range<Idx> {
102 #[inline]
121 #[stable(feature = "range_contains", since = "1.35.0")]
122 #[rustc_const_unstable(feature = "const_range", issue = "none")]
123 pub const fn contains<U>(&self, item: &U) -> bool
124 where
125 Idx: [const] PartialOrd<U>,
126 U: ?Sized + [const] PartialOrd<Idx>,
127 {
128 <Self as RangeBounds<Idx>>::contains(self, item)
129 }
130
131 #[inline]
149 #[stable(feature = "range_is_empty", since = "1.47.0")]
150 #[rustc_const_unstable(feature = "const_range", issue = "none")]
151 pub const fn is_empty(&self) -> bool
152 where
153 Idx: [const] PartialOrd<Idx>,
154 {
155 !(self.start < self.end)
156 }
157}
158
159#[lang = "RangeFrom"]
193#[doc(alias = "..")]
194#[derive(#[automatically_derived]
#[stable(feature = "rust1", since = "1.0.0")]
impl<Idx: crate::cmp::Eq> crate::cmp::Eq for RangeFrom<Idx> {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: crate::cmp::AssertParamIsEq<Idx>;
}
}Eq, #[automatically_derived]
#[stable(feature = "rust1", since = "1.0.0")]
impl<Idx: crate::hash::Hash> crate::hash::Hash for RangeFrom<Idx> {
#[inline]
fn hash<__H: crate::hash::Hasher>(&self, state: &mut __H) {
crate::hash::Hash::hash(&self.start, state)
}
}Hash)]
195#[derive_const(#[automatically_derived]
#[rustc_const_unstable(feature = "derive_const", issue = "118304")]
#[stable(feature = "rust1", since = "1.0.0")]
const impl<Idx: [const] crate::clone::Clone> crate::clone::Clone for
RangeFrom<Idx> {
#[inline]
fn clone(&self) -> RangeFrom<Idx> {
RangeFrom { start: crate::clone::Clone::clone(&self.start) }
}
}Clone, #[automatically_derived]
#[rustc_const_unstable(feature = "derive_const", issue = "118304")]
#[stable(feature = "rust1", since = "1.0.0")]
const impl<Idx: [const] crate::cmp::PartialEq> crate::cmp::PartialEq for
RangeFrom<Idx> {
#[inline]
fn eq(&self, other: &RangeFrom<Idx>) -> bool { self.start == other.start }
}PartialEq)] #[stable(feature = "rust1", since = "1.0.0")]
197pub struct RangeFrom<Idx> {
198 #[stable(feature = "rust1", since = "1.0.0")]
200 pub start: Idx,
201}
202
203#[stable(feature = "rust1", since = "1.0.0")]
204impl<Idx: fmt::Debug> fmt::Debug for RangeFrom<Idx> {
205 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
206 self.start.fmt(fmt)?;
207 fmt.write_fmt(format_args!(".."))write!(fmt, "..")?;
208 Ok(())
209 }
210}
211
212impl<Idx: PartialOrd<Idx>> RangeFrom<Idx> {
213 #[inline]
227 #[stable(feature = "range_contains", since = "1.35.0")]
228 #[rustc_const_unstable(feature = "const_range", issue = "none")]
229 pub const fn contains<U>(&self, item: &U) -> bool
230 where
231 Idx: [const] PartialOrd<U>,
232 U: ?Sized + [const] PartialOrd<Idx>,
233 {
234 <Self as RangeBounds<Idx>>::contains(self, item)
235 }
236}
237
238#[lang = "RangeTo"]
277#[doc(alias = "..")]
278#[derive(#[automatically_derived]
#[stable(feature = "rust1", since = "1.0.0")]
impl<Idx: crate::marker::Copy> crate::marker::Copy for RangeTo<Idx> { }Copy, #[automatically_derived]
#[stable(feature = "rust1", since = "1.0.0")]
impl<Idx: crate::cmp::Eq> crate::cmp::Eq for RangeTo<Idx> {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: crate::cmp::AssertParamIsEq<Idx>;
}
}Eq, #[automatically_derived]
#[stable(feature = "rust1", since = "1.0.0")]
impl<Idx: crate::hash::Hash> crate::hash::Hash for RangeTo<Idx> {
#[inline]
fn hash<__H: crate::hash::Hasher>(&self, state: &mut __H) {
crate::hash::Hash::hash(&self.end, state)
}
}Hash)]
279#[derive_const(#[automatically_derived]
#[rustc_const_unstable(feature = "derive_const", issue = "118304")]
#[stable(feature = "rust1", since = "1.0.0")]
const impl<Idx: [const] crate::clone::Clone> crate::clone::Clone for
RangeTo<Idx> {
#[inline]
fn clone(&self) -> RangeTo<Idx> {
RangeTo { end: crate::clone::Clone::clone(&self.end) }
}
}Clone, #[automatically_derived]
#[rustc_const_unstable(feature = "derive_const", issue = "118304")]
#[stable(feature = "rust1", since = "1.0.0")]
const impl<Idx: [const] crate::cmp::PartialEq> crate::cmp::PartialEq for
RangeTo<Idx> {
#[inline]
fn eq(&self, other: &RangeTo<Idx>) -> bool { self.end == other.end }
}PartialEq)]
280#[stable(feature = "rust1", since = "1.0.0")]
281pub struct RangeTo<Idx> {
282 #[stable(feature = "rust1", since = "1.0.0")]
284 pub end: Idx,
285}
286
287#[stable(feature = "rust1", since = "1.0.0")]
288impl<Idx: fmt::Debug> fmt::Debug for RangeTo<Idx> {
289 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
290 fmt.write_fmt(format_args!(".."))write!(fmt, "..")?;
291 self.end.fmt(fmt)?;
292 Ok(())
293 }
294}
295
296impl<Idx: PartialOrd<Idx>> RangeTo<Idx> {
297 #[inline]
311 #[stable(feature = "range_contains", since = "1.35.0")]
312 #[rustc_const_unstable(feature = "const_range", issue = "none")]
313 pub const fn contains<U>(&self, item: &U) -> bool
314 where
315 Idx: [const] PartialOrd<U>,
316 U: ?Sized + [const] PartialOrd<Idx>,
317 {
318 <Self as RangeBounds<Idx>>::contains(self, item)
319 }
320}
321
322#[lang = "RangeInclusive"]
353#[doc(alias = "..=")]
354#[derive(#[automatically_derived]
#[stable(feature = "inclusive_range", since = "1.26.0")]
impl<Idx: crate::clone::Clone> crate::clone::Clone for RangeInclusive<Idx> {
#[inline]
fn clone(&self) -> RangeInclusive<Idx> {
RangeInclusive {
start: crate::clone::Clone::clone(&self.start),
end: crate::clone::Clone::clone(&self.end),
exhausted: crate::clone::Clone::clone(&self.exhausted),
}
}
}Clone, #[automatically_derived]
#[stable(feature = "inclusive_range", since = "1.26.0")]
impl<Idx: crate::hash::Hash> crate::hash::Hash for RangeInclusive<Idx> {
#[inline]
fn hash<__H: crate::hash::Hasher>(&self, state: &mut __H) {
crate::hash::Hash::hash(&self.start, state);
crate::hash::Hash::hash(&self.end, state);
crate::hash::Hash::hash(&self.exhausted, state)
}
}Hash)]
355#[derive_const(#[automatically_derived]
#[rustc_const_unstable(feature = "derive_const", issue = "118304")]
#[stable(feature = "inclusive_range", since = "1.26.0")]
const impl<Idx: [const] crate::cmp::Eq> crate::cmp::Eq for RangeInclusive<Idx>
{
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: crate::cmp::AssertParamIsEq<Idx>;
let _: crate::cmp::AssertParamIsEq<bool>;
}
}Eq, #[automatically_derived]
#[rustc_const_unstable(feature = "derive_const", issue = "118304")]
#[stable(feature = "inclusive_range", since = "1.26.0")]
const impl<Idx: [const] crate::cmp::PartialEq> crate::cmp::PartialEq for
RangeInclusive<Idx> {
#[inline]
fn eq(&self, other: &RangeInclusive<Idx>) -> bool {
self.exhausted == other.exhausted && self.start == other.start &&
self.end == other.end
}
}PartialEq)] #[stable(feature = "inclusive_range", since = "1.26.0")]
357pub struct RangeInclusive<Idx> {
358 pub(crate) start: Idx,
364 pub(crate) end: Idx,
365
366 pub(crate) exhausted: bool,
378}
379
380impl<Idx> RangeInclusive<Idx> {
381 #[lang = "range_inclusive_new"]
391 #[stable(feature = "inclusive_range_methods", since = "1.27.0")]
392 #[inline]
393 #[rustc_promotable]
394 #[rustc_const_stable(feature = "const_range_new", since = "1.32.0")]
395 pub const fn new(start: Idx, end: Idx) -> Self {
396 Self { start, end, exhausted: false }
397 }
398
399 #[stable(feature = "inclusive_range_methods", since = "1.27.0")]
418 #[rustc_const_stable(feature = "const_inclusive_range_methods", since = "1.32.0")]
419 #[inline]
420 pub const fn start(&self) -> &Idx {
421 &self.start
422 }
423
424 #[stable(feature = "inclusive_range_methods", since = "1.27.0")]
443 #[rustc_const_stable(feature = "const_inclusive_range_methods", since = "1.32.0")]
444 #[inline]
445 pub const fn end(&self) -> &Idx {
446 &self.end
447 }
448
449 #[stable(feature = "inclusive_range_methods", since = "1.27.0")]
460 #[inline]
461 #[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")]
462 pub const fn into_inner(self) -> (Idx, Idx) {
463 (self.start, self.end)
464 }
465}
466
467impl RangeInclusive<usize> {
468 #[inline]
471 pub(crate) const fn into_slice_range(self) -> Range<usize> {
472 let exclusive_end = self.end + 1;
483 let start = if self.exhausted { exclusive_end } else { self.start };
484 start..exclusive_end
485 }
486}
487
488#[stable(feature = "inclusive_range", since = "1.26.0")]
489impl<Idx: fmt::Debug> fmt::Debug for RangeInclusive<Idx> {
490 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
491 self.start.fmt(fmt)?;
492 fmt.write_fmt(format_args!("..="))write!(fmt, "..=")?;
493 self.end.fmt(fmt)?;
494 if self.exhausted {
495 fmt.write_fmt(format_args!(" (exhausted)"))write!(fmt, " (exhausted)")?;
496 }
497 Ok(())
498 }
499}
500
501impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> {
502 #[inline]
532 #[stable(feature = "range_contains", since = "1.35.0")]
533 #[rustc_const_unstable(feature = "const_range", issue = "none")]
534 pub const fn contains<U>(&self, item: &U) -> bool
535 where
536 Idx: [const] PartialOrd<U>,
537 U: ?Sized + [const] PartialOrd<Idx>,
538 {
539 <Self as RangeBounds<Idx>>::contains(self, item)
540 }
541
542 #[stable(feature = "range_is_empty", since = "1.47.0")]
569 #[inline]
570 #[rustc_const_unstable(feature = "const_range", issue = "none")]
571 pub const fn is_empty(&self) -> bool
572 where
573 Idx: [const] PartialOrd,
574 {
575 self.exhausted || !(self.start <= self.end)
576 }
577}
578
579#[lang = "RangeToInclusive"]
618#[doc(alias = "..=")]
619#[derive(#[automatically_derived]
#[stable(feature = "inclusive_range", since = "1.26.0")]
impl<Idx: crate::marker::Copy> crate::marker::Copy for RangeToInclusive<Idx> {
}Copy, #[automatically_derived]
#[stable(feature = "inclusive_range", since = "1.26.0")]
impl<Idx: crate::hash::Hash> crate::hash::Hash for RangeToInclusive<Idx> {
#[inline]
fn hash<__H: crate::hash::Hasher>(&self, state: &mut __H) {
crate::hash::Hash::hash(&self.end, state)
}
}Hash)]
620#[derive(#[automatically_derived]
#[stable(feature = "inclusive_range", since = "1.26.0")]
impl<Idx: crate::clone::Clone> crate::clone::Clone for RangeToInclusive<Idx> {
#[inline]
fn clone(&self) -> RangeToInclusive<Idx> {
RangeToInclusive { end: crate::clone::Clone::clone(&self.end) }
}
}Clone, #[automatically_derived]
#[stable(feature = "inclusive_range", since = "1.26.0")]
impl<Idx: crate::cmp::PartialEq> crate::cmp::PartialEq for
RangeToInclusive<Idx> {
#[inline]
fn eq(&self, other: &RangeToInclusive<Idx>) -> bool {
self.end == other.end
}
}PartialEq, #[automatically_derived]
#[stable(feature = "inclusive_range", since = "1.26.0")]
impl<Idx: crate::cmp::Eq> crate::cmp::Eq for RangeToInclusive<Idx> {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: crate::cmp::AssertParamIsEq<Idx>;
}
}Eq)]
621#[stable(feature = "inclusive_range", since = "1.26.0")]
622pub struct RangeToInclusive<Idx> {
623 #[stable(feature = "inclusive_range", since = "1.26.0")]
625 pub end: Idx,
626}
627
628#[stable(feature = "inclusive_range", since = "1.26.0")]
629impl<Idx: fmt::Debug> fmt::Debug for RangeToInclusive<Idx> {
630 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
631 fmt.write_fmt(format_args!("..="))write!(fmt, "..=")?;
632 self.end.fmt(fmt)?;
633 Ok(())
634 }
635}
636
637impl<Idx: PartialOrd<Idx>> RangeToInclusive<Idx> {
638 #[inline]
652 #[stable(feature = "range_contains", since = "1.35.0")]
653 #[rustc_const_unstable(feature = "const_range", issue = "none")]
654 pub const fn contains<U>(&self, item: &U) -> bool
655 where
656 Idx: [const] PartialOrd<U>,
657 U: ?Sized + [const] PartialOrd<Idx>,
658 {
659 <Self as RangeBounds<Idx>>::contains(self, item)
660 }
661}
662
663#[stable(feature = "collections_bound", since = "1.17.0")]
702#[derive(#[automatically_derived]
#[stable(feature = "collections_bound", since = "1.17.0")]
impl<T: crate::marker::Copy> crate::marker::Copy for Bound<T> { }Copy, #[automatically_derived]
#[stable(feature = "collections_bound", since = "1.17.0")]
impl<T: crate::fmt::Debug> crate::fmt::Debug for Bound<T> {
#[inline]
fn fmt(&self, f: &mut crate::fmt::Formatter) -> crate::fmt::Result {
match self {
Bound::Included(__self_0) =>
crate::fmt::Formatter::debug_tuple_field1_finish(f,
"Included", &__self_0),
Bound::Excluded(__self_0) =>
crate::fmt::Formatter::debug_tuple_field1_finish(f,
"Excluded", &__self_0),
Bound::Unbounded =>
crate::fmt::Formatter::write_str(f, "Unbounded"),
}
}
}Debug, #[automatically_derived]
#[stable(feature = "collections_bound", since = "1.17.0")]
impl<T: crate::hash::Hash> crate::hash::Hash for Bound<T> {
#[inline]
fn hash<__H: crate::hash::Hasher>(&self, state: &mut __H) {
let __self_discr = crate::intrinsics::discriminant_value(self);
crate::hash::Hash::hash(&__self_discr, state);
match self {
Bound::Included(__self_0) =>
crate::hash::Hash::hash(__self_0, state),
Bound::Excluded(__self_0) =>
crate::hash::Hash::hash(__self_0, state),
_ => {}
}
}
}Hash)]
703#[derive_const(#[automatically_derived]
#[rustc_const_unstable(feature = "derive_const", issue = "118304")]
#[stable(feature = "collections_bound", since = "1.17.0")]
const impl<T: [const] crate::clone::Clone> crate::clone::Clone for Bound<T> {
#[inline]
fn clone(&self) -> Bound<T> {
match self {
Bound::Included(__self_0) =>
Bound::Included(crate::clone::Clone::clone(__self_0)),
Bound::Excluded(__self_0) =>
Bound::Excluded(crate::clone::Clone::clone(__self_0)),
Bound::Unbounded => Bound::Unbounded,
}
}
}Clone, #[automatically_derived]
#[rustc_const_unstable(feature = "derive_const", issue = "118304")]
#[stable(feature = "collections_bound", since = "1.17.0")]
const impl<T: [const] crate::cmp::Eq> crate::cmp::Eq for Bound<T> {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) { let _: crate::cmp::AssertParamIsEq<T>; }
}Eq, #[automatically_derived]
#[rustc_const_unstable(feature = "derive_const", issue = "118304")]
#[stable(feature = "collections_bound", since = "1.17.0")]
const impl<T: [const] crate::cmp::PartialEq> crate::cmp::PartialEq for
Bound<T> {
#[inline]
fn eq(&self, other: &Bound<T>) -> bool {
let __self_discr = crate::intrinsics::discriminant_value(self);
let __arg1_discr = crate::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr &&
match (self, other) {
(Bound::Included(__self_0), Bound::Included(__arg1_0)) =>
__self_0 == __arg1_0,
(Bound::Excluded(__self_0), Bound::Excluded(__arg1_0)) =>
__self_0 == __arg1_0,
_ => true,
}
}
}PartialEq)]
704pub enum Bound<T> {
705 #[stable(feature = "collections_bound", since = "1.17.0")]
707 Included(#[stable(feature = "collections_bound", since = "1.17.0")] T),
708 #[stable(feature = "collections_bound", since = "1.17.0")]
710 Excluded(#[stable(feature = "collections_bound", since = "1.17.0")] T),
711 #[stable(feature = "collections_bound", since = "1.17.0")]
713 Unbounded,
714}
715
716impl<T> Bound<T> {
717 #[inline]
719 #[stable(feature = "bound_as_ref_shared", since = "1.65.0")]
720 #[rustc_const_unstable(feature = "const_range", issue = "none")]
721 pub const fn as_ref(&self) -> Bound<&T> {
722 match *self {
723 Included(ref x) => Included(x),
724 Excluded(ref x) => Excluded(x),
725 Unbounded => Unbounded,
726 }
727 }
728
729 #[inline]
731 #[unstable(feature = "bound_as_ref", issue = "80996")]
732 pub const fn as_mut(&mut self) -> Bound<&mut T> {
733 match *self {
734 Included(ref mut x) => Included(x),
735 Excluded(ref mut x) => Excluded(x),
736 Unbounded => Unbounded,
737 }
738 }
739
740 #[inline]
762 #[stable(feature = "bound_map", since = "1.77.0")]
763 pub fn map<U, F: FnOnce(T) -> U>(self, f: F) -> Bound<U> {
764 match self {
765 Unbounded => Unbounded,
766 Included(x) => Included(f(x)),
767 Excluded(x) => Excluded(f(x)),
768 }
769 }
770}
771
772impl<T: Copy> Bound<&T> {
773 #[unstable(feature = "bound_copied", issue = "145966")]
787 #[must_use]
788 pub const fn copied(self) -> Bound<T> {
789 match self {
790 Bound::Unbounded => Bound::Unbounded,
791 Bound::Included(x) => Bound::Included(*x),
792 Bound::Excluded(x) => Bound::Excluded(*x),
793 }
794 }
795}
796
797impl<T: Clone> Bound<&T> {
798 #[must_use = "`self` will be dropped if the result is not used"]
813 #[stable(feature = "bound_cloned", since = "1.55.0")]
814 #[rustc_const_unstable(feature = "const_range", issue = "none")]
815 pub const fn cloned(self) -> Bound<T>
816 where
817 T: [const] Clone,
818 {
819 match self {
820 Bound::Unbounded => Bound::Unbounded,
821 Bound::Included(x) => Bound::Included(x.clone()),
822 Bound::Excluded(x) => Bound::Excluded(x.clone()),
823 }
824 }
825}
826
827#[stable(feature = "collections_range", since = "1.28.0")]
830#[rustc_diagnostic_item = "RangeBounds"]
831#[rustc_const_unstable(feature = "const_range", issue = "none")]
832pub const trait RangeBounds<T: ?Sized> {
833 #[stable(feature = "collections_range", since = "1.28.0")]
847 fn start_bound(&self) -> Bound<&T>;
848
849 #[stable(feature = "collections_range", since = "1.28.0")]
863 fn end_bound(&self) -> Bound<&T>;
864
865 #[inline]
879 #[stable(feature = "range_contains", since = "1.35.0")]
880 fn contains<U>(&self, item: &U) -> bool
881 where
882 T: [const] PartialOrd<U>,
883 U: ?Sized + [const] PartialOrd<T>,
884 {
885 (match self.start_bound() {
886 Included(start) => start <= item,
887 Excluded(start) => start < item,
888 Unbounded => true,
889 }) && (match self.end_bound() {
890 Included(end) => item <= end,
891 Excluded(end) => item < end,
892 Unbounded => true,
893 })
894 }
895
896 #[unstable(feature = "range_bounds_is_empty", issue = "137300")]
948 fn is_empty(&self) -> bool
949 where
950 T: [const] PartialOrd,
951 {
952 !match (self.start_bound(), self.end_bound()) {
953 (Unbounded, _) | (_, Unbounded) => true,
954 (Included(start), Excluded(end))
955 | (Excluded(start), Included(end))
956 | (Excluded(start), Excluded(end)) => start < end,
957 (Included(start), Included(end)) => start <= end,
958 }
959 }
960}
961
962#[unstable(feature = "range_into_bounds", issue = "136903")]
968#[rustc_const_unstable(feature = "const_range", issue = "none")]
969pub const trait IntoBounds<T>: [const] RangeBounds<T> {
970 fn into_bounds(self) -> (Bound<T>, Bound<T>);
984
985 fn intersect<R>(self, other: R) -> (Bound<T>, Bound<T>)
1013 where
1014 Self: Sized,
1015 T: [const] Ord + [const] Destruct,
1016 R: Sized + [const] IntoBounds<T>,
1017 {
1018 let (self_start, self_end) = IntoBounds::into_bounds(self);
1019 let (other_start, other_end) = IntoBounds::into_bounds(other);
1020
1021 let start = match (self_start, other_start) {
1022 (Included(a), Included(b)) => Included(Ord::max(a, b)),
1023 (Excluded(a), Excluded(b)) => Excluded(Ord::max(a, b)),
1024 (Unbounded, Unbounded) => Unbounded,
1025
1026 (x, Unbounded) | (Unbounded, x) => x,
1027
1028 (Included(i), Excluded(e)) | (Excluded(e), Included(i)) => {
1029 if i > e {
1030 Included(i)
1031 } else {
1032 Excluded(e)
1033 }
1034 }
1035 };
1036 let end = match (self_end, other_end) {
1037 (Included(a), Included(b)) => Included(Ord::min(a, b)),
1038 (Excluded(a), Excluded(b)) => Excluded(Ord::min(a, b)),
1039 (Unbounded, Unbounded) => Unbounded,
1040
1041 (x, Unbounded) | (Unbounded, x) => x,
1042
1043 (Included(i), Excluded(e)) | (Excluded(e), Included(i)) => {
1044 if i < e {
1045 Included(i)
1046 } else {
1047 Excluded(e)
1048 }
1049 }
1050 };
1051
1052 (start, end)
1053 }
1054}
1055
1056use self::Bound::{Excluded, Included, Unbounded};
1057
1058#[stable(feature = "collections_range", since = "1.28.0")]
1059#[rustc_const_unstable(feature = "const_range", issue = "none")]
1060const impl<T: ?Sized> RangeBounds<T> for RangeFull {
1061 fn start_bound(&self) -> Bound<&T> {
1062 Unbounded
1063 }
1064 fn end_bound(&self) -> Bound<&T> {
1065 Unbounded
1066 }
1067}
1068
1069#[unstable(feature = "range_into_bounds", issue = "136903")]
1070#[rustc_const_unstable(feature = "const_range", issue = "none")]
1071const impl<T> IntoBounds<T> for RangeFull {
1072 fn into_bounds(self) -> (Bound<T>, Bound<T>) {
1073 (Unbounded, Unbounded)
1074 }
1075}
1076
1077#[stable(feature = "collections_range", since = "1.28.0")]
1078#[rustc_const_unstable(feature = "const_range", issue = "none")]
1079const impl<T> RangeBounds<T> for RangeFrom<T> {
1080 fn start_bound(&self) -> Bound<&T> {
1081 Included(&self.start)
1082 }
1083 fn end_bound(&self) -> Bound<&T> {
1084 Unbounded
1085 }
1086}
1087
1088#[unstable(feature = "range_into_bounds", issue = "136903")]
1089#[rustc_const_unstable(feature = "const_range", issue = "none")]
1090const impl<T> IntoBounds<T> for RangeFrom<T> {
1091 fn into_bounds(self) -> (Bound<T>, Bound<T>) {
1092 (Included(self.start), Unbounded)
1093 }
1094}
1095
1096#[stable(feature = "collections_range", since = "1.28.0")]
1097#[rustc_const_unstable(feature = "const_range", issue = "none")]
1098const impl<T> RangeBounds<T> for RangeTo<T> {
1099 fn start_bound(&self) -> Bound<&T> {
1100 Unbounded
1101 }
1102 fn end_bound(&self) -> Bound<&T> {
1103 Excluded(&self.end)
1104 }
1105}
1106
1107#[unstable(feature = "range_into_bounds", issue = "136903")]
1108#[rustc_const_unstable(feature = "const_range", issue = "none")]
1109const impl<T> IntoBounds<T> for RangeTo<T> {
1110 fn into_bounds(self) -> (Bound<T>, Bound<T>) {
1111 (Unbounded, Excluded(self.end))
1112 }
1113}
1114
1115#[stable(feature = "collections_range", since = "1.28.0")]
1116#[rustc_const_unstable(feature = "const_range", issue = "none")]
1117const impl<T> RangeBounds<T> for Range<T> {
1118 fn start_bound(&self) -> Bound<&T> {
1119 Included(&self.start)
1120 }
1121 fn end_bound(&self) -> Bound<&T> {
1122 Excluded(&self.end)
1123 }
1124}
1125
1126#[unstable(feature = "range_into_bounds", issue = "136903")]
1127#[rustc_const_unstable(feature = "const_range", issue = "none")]
1128const impl<T> IntoBounds<T> for Range<T> {
1129 fn into_bounds(self) -> (Bound<T>, Bound<T>) {
1130 (Included(self.start), Excluded(self.end))
1131 }
1132}
1133
1134#[stable(feature = "collections_range", since = "1.28.0")]
1135#[rustc_const_unstable(feature = "const_range", issue = "none")]
1136const impl<T> RangeBounds<T> for RangeInclusive<T> {
1137 fn start_bound(&self) -> Bound<&T> {
1138 Included(&self.start)
1139 }
1140 fn end_bound(&self) -> Bound<&T> {
1141 if self.exhausted {
1142 Excluded(&self.start)
1147 } else {
1148 Included(&self.end)
1149 }
1150 }
1151}
1152
1153#[unstable(feature = "range_into_bounds", issue = "136903")]
1154#[rustc_const_unstable(feature = "const_range", issue = "none")]
1155const impl<T> IntoBounds<T> for RangeInclusive<T> {
1156 fn into_bounds(self) -> (Bound<T>, Bound<T>) {
1157 if !!self.exhausted {
{
crate::panicking::panic_fmt(format_args!("attempted to convert from an exhausted `RangeInclusive` (unspecified behavior)"));
}
};assert!(
1158 !self.exhausted,
1159 "attempted to convert from an exhausted `RangeInclusive` (unspecified behavior)"
1160 );
1161
1162 (Included(self.start), Included(self.end))
1163 }
1164}
1165
1166#[stable(feature = "collections_range", since = "1.28.0")]
1167#[rustc_const_unstable(feature = "const_range", issue = "none")]
1168const impl<T> RangeBounds<T> for RangeToInclusive<T> {
1169 fn start_bound(&self) -> Bound<&T> {
1170 Unbounded
1171 }
1172 fn end_bound(&self) -> Bound<&T> {
1173 Included(&self.end)
1174 }
1175}
1176
1177#[unstable(feature = "range_into_bounds", issue = "136903")]
1178#[rustc_const_unstable(feature = "const_range", issue = "none")]
1179const impl<T> IntoBounds<T> for RangeToInclusive<T> {
1180 fn into_bounds(self) -> (Bound<T>, Bound<T>) {
1181 (Unbounded, Included(self.end))
1182 }
1183}
1184
1185#[stable(feature = "collections_range", since = "1.28.0")]
1186#[rustc_const_unstable(feature = "const_range", issue = "none")]
1187const impl<T> RangeBounds<T> for (Bound<T>, Bound<T>) {
1188 fn start_bound(&self) -> Bound<&T> {
1189 match *self {
1190 (Included(ref start), _) => Included(start),
1191 (Excluded(ref start), _) => Excluded(start),
1192 (Unbounded, _) => Unbounded,
1193 }
1194 }
1195
1196 fn end_bound(&self) -> Bound<&T> {
1197 match *self {
1198 (_, Included(ref end)) => Included(end),
1199 (_, Excluded(ref end)) => Excluded(end),
1200 (_, Unbounded) => Unbounded,
1201 }
1202 }
1203}
1204
1205#[unstable(feature = "range_into_bounds", issue = "136903")]
1206#[rustc_const_unstable(feature = "const_range", issue = "none")]
1207const impl<T> IntoBounds<T> for (Bound<T>, Bound<T>) {
1208 fn into_bounds(self) -> (Bound<T>, Bound<T>) {
1209 self
1210 }
1211}
1212
1213#[stable(feature = "collections_range", since = "1.28.0")]
1214#[rustc_const_unstable(feature = "const_range", issue = "none")]
1215const impl<'a, T: ?Sized + 'a> RangeBounds<T> for (Bound<&'a T>, Bound<&'a T>) {
1216 fn start_bound(&self) -> Bound<&T> {
1217 self.0
1218 }
1219
1220 fn end_bound(&self) -> Bound<&T> {
1221 self.1
1222 }
1223}
1224
1225#[stable(feature = "collections_range", since = "1.28.0")]
1232#[rustc_const_unstable(feature = "const_range", issue = "none")]
1233const impl<T> RangeBounds<T> for RangeFrom<&T> {
1234 fn start_bound(&self) -> Bound<&T> {
1235 Included(self.start)
1236 }
1237 fn end_bound(&self) -> Bound<&T> {
1238 Unbounded
1239 }
1240}
1241
1242#[stable(feature = "collections_range", since = "1.28.0")]
1249#[rustc_const_unstable(feature = "const_range", issue = "none")]
1250const impl<T> RangeBounds<T> for RangeTo<&T> {
1251 fn start_bound(&self) -> Bound<&T> {
1252 Unbounded
1253 }
1254 fn end_bound(&self) -> Bound<&T> {
1255 Excluded(self.end)
1256 }
1257}
1258
1259#[stable(feature = "collections_range", since = "1.28.0")]
1266#[rustc_const_unstable(feature = "const_range", issue = "none")]
1267const impl<T> RangeBounds<T> for Range<&T> {
1268 fn start_bound(&self) -> Bound<&T> {
1269 Included(self.start)
1270 }
1271 fn end_bound(&self) -> Bound<&T> {
1272 Excluded(self.end)
1273 }
1274}
1275
1276#[stable(feature = "collections_range", since = "1.28.0")]
1283#[rustc_const_unstable(feature = "const_range", issue = "none")]
1284const impl<T> RangeBounds<T> for RangeInclusive<&T> {
1285 fn start_bound(&self) -> Bound<&T> {
1286 Included(self.start)
1287 }
1288 fn end_bound(&self) -> Bound<&T> {
1289 Included(self.end)
1290 }
1291}
1292
1293#[stable(feature = "collections_range", since = "1.28.0")]
1300#[rustc_const_unstable(feature = "const_range", issue = "none")]
1301const impl<T> RangeBounds<T> for RangeToInclusive<&T> {
1302 fn start_bound(&self) -> Bound<&T> {
1303 Unbounded
1304 }
1305 fn end_bound(&self) -> Bound<&T> {
1306 Included(self.end)
1307 }
1308}
1309
1310#[unstable(feature = "one_sided_range", issue = "69780")]
1313#[allow(missing_debug_implementations)]
1314pub enum OneSidedRangeBound {
1315 StartInclusive,
1317 End,
1319 EndInclusive,
1321}
1322
1323#[unstable(feature = "one_sided_range", issue = "69780")]
1330#[rustc_const_unstable(feature = "const_range", issue = "none")]
1331pub const trait OneSidedRange<T>: RangeBounds<T> {
1332 fn bound(self) -> (OneSidedRangeBound, T);
1335}
1336
1337#[unstable(feature = "one_sided_range", issue = "69780")]
1338#[rustc_const_unstable(feature = "const_range", issue = "none")]
1339const impl<T> OneSidedRange<T> for RangeTo<T>
1340where
1341 Self: RangeBounds<T>,
1342{
1343 fn bound(self) -> (OneSidedRangeBound, T) {
1344 (OneSidedRangeBound::End, self.end)
1345 }
1346}
1347
1348#[unstable(feature = "one_sided_range", issue = "69780")]
1349#[rustc_const_unstable(feature = "const_range", issue = "none")]
1350const impl<T> OneSidedRange<T> for RangeFrom<T>
1351where
1352 Self: RangeBounds<T>,
1353{
1354 fn bound(self) -> (OneSidedRangeBound, T) {
1355 (OneSidedRangeBound::StartInclusive, self.start)
1356 }
1357}
1358
1359#[unstable(feature = "one_sided_range", issue = "69780")]
1360#[rustc_const_unstable(feature = "const_range", issue = "none")]
1361const impl<T> OneSidedRange<T> for RangeToInclusive<T>
1362where
1363 Self: RangeBounds<T>,
1364{
1365 fn bound(self) -> (OneSidedRangeBound, T) {
1366 (OneSidedRangeBound::EndInclusive, self.end)
1367 }
1368}