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")]
#[doc(hidden)]
#[stable(feature = "rust1", since = "1.0.0")]
const unsafe impl crate::clone::TrivialClone for RangeFull { }
#[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) -> Self { *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() -> Self { 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 { }Eq, #[automatically_derived]
#[stable(feature = "rust1", since = "1.0.0")]
impl crate::marker::StructuralPartialEq for RangeFull { }
#[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: &Self) -> 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"]
82#[doc(alias = "..")]
83#[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)]
84#[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) -> Self {
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() -> Self {
Range {
start: crate::default::Default::default(),
end: crate::default::Default::default(),
}
}
}Default, #[automatically_derived]
#[stable(feature = "rust1", since = "1.0.0")]
impl<Idx: crate::cmp::PartialEq> crate::marker::StructuralPartialEq for
Range<Idx> {
}
#[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: &Self) -> bool {
self.start == other.start && self.end == other.end
}
}PartialEq)] #[stable(feature = "rust1", since = "1.0.0")]
86pub struct Range<Idx> {
87 #[stable(feature = "rust1", since = "1.0.0")]
89 pub start: Idx,
90 #[stable(feature = "rust1", since = "1.0.0")]
92 pub end: Idx,
93}
94
95#[stable(feature = "rust1", since = "1.0.0")]
96impl<Idx: fmt::Debug> fmt::Debug for Range<Idx> {
97 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
98 self.start.fmt(fmt)?;
99 fmt.write_fmt(format_args!(".."))write!(fmt, "..")?;
100 self.end.fmt(fmt)?;
101 Ok(())
102 }
103}
104
105impl<Idx: PartialOrd<Idx>> Range<Idx> {
106 #[inline]
125 #[stable(feature = "range_contains", since = "1.35.0")]
126 #[rustc_const_unstable(feature = "const_range", issue = "none")]
127 pub const fn contains<U>(&self, item: &U) -> bool
128 where
129 Idx: [const] PartialOrd<U>,
130 U: ?Sized + [const] PartialOrd<Idx>,
131 {
132 <Self as RangeBounds<Idx>>::contains(self, item)
133 }
134
135 #[inline]
153 #[stable(feature = "range_is_empty", since = "1.47.0")]
154 #[rustc_const_unstable(feature = "const_range", issue = "none")]
155 #[expect(clippy::neg_cmp_op_on_partial_ord, reason = "incomparable ranges are empty")]
156 pub const fn is_empty(&self) -> bool
157 where
158 Idx: [const] PartialOrd<Idx>,
159 {
160 !(self.start < self.end)
161 }
162}
163
164#[lang = "RangeFrom"]
198#[doc(alias = "..")]
199#[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)]
200#[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) -> Self {
RangeFrom { start: crate::clone::Clone::clone(&self.start) }
}
}Clone, #[automatically_derived]
#[stable(feature = "rust1", since = "1.0.0")]
impl<Idx: crate::cmp::PartialEq> crate::marker::StructuralPartialEq for
RangeFrom<Idx> {
}
#[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: &Self) -> bool { self.start == other.start }
}PartialEq)] #[stable(feature = "rust1", since = "1.0.0")]
202pub struct RangeFrom<Idx> {
203 #[stable(feature = "rust1", since = "1.0.0")]
205 pub start: Idx,
206}
207
208#[stable(feature = "rust1", since = "1.0.0")]
209impl<Idx: fmt::Debug> fmt::Debug for RangeFrom<Idx> {
210 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
211 self.start.fmt(fmt)?;
212 fmt.write_fmt(format_args!(".."))write!(fmt, "..")?;
213 Ok(())
214 }
215}
216
217impl<Idx: PartialOrd<Idx>> RangeFrom<Idx> {
218 #[inline]
232 #[stable(feature = "range_contains", since = "1.35.0")]
233 #[rustc_const_unstable(feature = "const_range", issue = "none")]
234 pub const fn contains<U>(&self, item: &U) -> bool
235 where
236 Idx: [const] PartialOrd<U>,
237 U: ?Sized + [const] PartialOrd<Idx>,
238 {
239 <Self as RangeBounds<Idx>>::contains(self, item)
240 }
241}
242
243#[lang = "RangeTo"]
282#[doc(alias = "..")]
283#[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)]
284#[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) -> Self {
RangeTo { end: crate::clone::Clone::clone(&self.end) }
}
}Clone, #[automatically_derived]
#[stable(feature = "rust1", since = "1.0.0")]
impl<Idx: crate::cmp::PartialEq> crate::marker::StructuralPartialEq for
RangeTo<Idx> {
}
#[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: &Self) -> bool { self.end == other.end }
}PartialEq)]
285#[stable(feature = "rust1", since = "1.0.0")]
286pub struct RangeTo<Idx> {
287 #[stable(feature = "rust1", since = "1.0.0")]
289 pub end: Idx,
290}
291
292#[stable(feature = "rust1", since = "1.0.0")]
293impl<Idx: fmt::Debug> fmt::Debug for RangeTo<Idx> {
294 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
295 fmt.write_fmt(format_args!(".."))write!(fmt, "..")?;
296 self.end.fmt(fmt)?;
297 Ok(())
298 }
299}
300
301impl<Idx: PartialOrd<Idx>> RangeTo<Idx> {
302 #[inline]
316 #[stable(feature = "range_contains", since = "1.35.0")]
317 #[rustc_const_unstable(feature = "const_range", issue = "none")]
318 pub const fn contains<U>(&self, item: &U) -> bool
319 where
320 Idx: [const] PartialOrd<U>,
321 U: ?Sized + [const] PartialOrd<Idx>,
322 {
323 <Self as RangeBounds<Idx>>::contains(self, item)
324 }
325}
326
327#[lang = "RangeInclusive"]
358#[doc(alias = "..=")]
359#[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) -> Self {
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)]
360#[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]
#[stable(feature = "inclusive_range", since = "1.26.0")]
impl<Idx: crate::cmp::PartialEq> crate::marker::StructuralPartialEq for
RangeInclusive<Idx> {
}
#[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: &Self) -> bool {
self.exhausted == other.exhausted && self.start == other.start &&
self.end == other.end
}
}PartialEq)] #[stable(feature = "inclusive_range", since = "1.26.0")]
362pub struct RangeInclusive<Idx> {
363 pub(crate) start: Idx,
369 pub(crate) end: Idx,
370
371 pub(crate) exhausted: bool,
383}
384
385impl<Idx> RangeInclusive<Idx> {
386 #[lang = "range_inclusive_new"]
396 #[stable(feature = "inclusive_range_methods", since = "1.27.0")]
397 #[inline]
398 #[rustc_promotable]
399 #[rustc_const_stable(feature = "const_range_new", since = "1.32.0")]
400 pub const fn new(start: Idx, end: Idx) -> Self {
401 Self { start, end, exhausted: false }
402 }
403
404 #[stable(feature = "inclusive_range_methods", since = "1.27.0")]
423 #[rustc_const_stable(feature = "const_inclusive_range_methods", since = "1.32.0")]
424 #[inline]
425 pub const fn start(&self) -> &Idx {
426 &self.start
427 }
428
429 #[stable(feature = "inclusive_range_methods", since = "1.27.0")]
448 #[rustc_const_stable(feature = "const_inclusive_range_methods", since = "1.32.0")]
449 #[inline]
450 pub const fn end(&self) -> &Idx {
451 &self.end
452 }
453
454 #[stable(feature = "inclusive_range_methods", since = "1.27.0")]
465 #[inline]
466 #[rustc_const_unstable(feature = "const_range_bounds", issue = "108082")]
467 pub const fn into_inner(self) -> (Idx, Idx) {
468 (self.start, self.end)
469 }
470}
471
472impl RangeInclusive<usize> {
473 #[inline]
476 pub(crate) const fn into_slice_range(self) -> Range<usize> {
477 let exclusive_end = self.end + 1;
488 let start = if self.exhausted { exclusive_end } else { self.start };
489 start..exclusive_end
490 }
491}
492
493#[stable(feature = "inclusive_range", since = "1.26.0")]
494impl<Idx: fmt::Debug> fmt::Debug for RangeInclusive<Idx> {
495 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
496 self.start.fmt(fmt)?;
497 fmt.write_fmt(format_args!("..="))write!(fmt, "..=")?;
498 self.end.fmt(fmt)?;
499 if self.exhausted {
500 fmt.write_fmt(format_args!(" (exhausted)"))write!(fmt, " (exhausted)")?;
501 }
502 Ok(())
503 }
504}
505
506impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> {
507 #[inline]
537 #[stable(feature = "range_contains", since = "1.35.0")]
538 #[rustc_const_unstable(feature = "const_range", issue = "none")]
539 pub const fn contains<U>(&self, item: &U) -> bool
540 where
541 Idx: [const] PartialOrd<U>,
542 U: ?Sized + [const] PartialOrd<Idx>,
543 {
544 <Self as RangeBounds<Idx>>::contains(self, item)
545 }
546
547 #[stable(feature = "range_is_empty", since = "1.47.0")]
574 #[inline]
575 #[rustc_const_unstable(feature = "const_range", issue = "none")]
576 #[expect(clippy::neg_cmp_op_on_partial_ord, reason = "incomparable ranges are empty")]
577 pub const fn is_empty(&self) -> bool
578 where
579 Idx: [const] PartialOrd,
580 {
581 self.exhausted || !(self.start <= self.end)
582 }
583}
584
585#[lang = "RangeToInclusive"]
624#[doc(alias = "..=")]
625#[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)]
626#[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) -> Self {
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::marker::StructuralPartialEq for
RangeToInclusive<Idx> {
}
#[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: &Self) -> 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)]
627#[stable(feature = "inclusive_range", since = "1.26.0")]
628pub struct RangeToInclusive<Idx> {
629 #[stable(feature = "inclusive_range", since = "1.26.0")]
631 pub end: Idx,
632}
633
634#[stable(feature = "inclusive_range", since = "1.26.0")]
635impl<Idx: fmt::Debug> fmt::Debug for RangeToInclusive<Idx> {
636 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
637 fmt.write_fmt(format_args!("..="))write!(fmt, "..=")?;
638 self.end.fmt(fmt)?;
639 Ok(())
640 }
641}
642
643impl<Idx: PartialOrd<Idx>> RangeToInclusive<Idx> {
644 #[inline]
658 #[stable(feature = "range_contains", since = "1.35.0")]
659 #[rustc_const_unstable(feature = "const_range", issue = "none")]
660 pub const fn contains<U>(&self, item: &U) -> bool
661 where
662 Idx: [const] PartialOrd<U>,
663 U: ?Sized + [const] PartialOrd<Idx>,
664 {
665 <Self as RangeBounds<Idx>>::contains(self, item)
666 }
667}
668
669#[stable(feature = "collections_bound", since = "1.17.0")]
708#[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 {
Self::Included(__self_0) =>
crate::fmt::Formatter::debug_tuple_field1_finish(f,
"Included", &__self_0),
Self::Excluded(__self_0) =>
crate::fmt::Formatter::debug_tuple_field1_finish(f,
"Excluded", &__self_0),
Self::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) {
crate::hash::Hash::hash(&crate::intrinsics::discriminant_value(self),
state);
match self {
Self::Included(__self_0) =>
crate::hash::Hash::hash(__self_0, state),
Self::Excluded(__self_0) =>
crate::hash::Hash::hash(__self_0, state),
_ => {}
}
}
}Hash)]
709#[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) -> Self {
match self {
Self::Included(__self_0) =>
Bound::Included(crate::clone::Clone::clone(__self_0)),
Self::Excluded(__self_0) =>
Bound::Excluded(crate::clone::Clone::clone(__self_0)),
Self::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]
#[stable(feature = "collections_bound", since = "1.17.0")]
impl<T: crate::cmp::PartialEq> crate::marker::StructuralPartialEq for Bound<T>
{
}
#[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: &Self) -> bool {
crate::intrinsics::discriminant_value(self) ==
crate::intrinsics::discriminant_value(other) &&
match (self, other) {
(Self::Included(__self_0), Self::Included(__arg1_0)) =>
__self_0 == __arg1_0,
(Self::Excluded(__self_0), Self::Excluded(__arg1_0)) =>
__self_0 == __arg1_0,
_ => true,
}
}
}PartialEq)]
710pub enum Bound<T> {
711 #[stable(feature = "collections_bound", since = "1.17.0")]
713 Included(#[stable(feature = "collections_bound", since = "1.17.0")] T),
714 #[stable(feature = "collections_bound", since = "1.17.0")]
716 Excluded(#[stable(feature = "collections_bound", since = "1.17.0")] T),
717 #[stable(feature = "collections_bound", since = "1.17.0")]
719 Unbounded,
720}
721
722impl<T> Bound<T> {
723 #[inline]
725 #[stable(feature = "bound_as_ref_shared", since = "1.65.0")]
726 #[rustc_const_unstable(feature = "const_range", issue = "none")]
727 pub const fn as_ref(&self) -> Bound<&T> {
728 match *self {
729 Included(ref x) => Included(x),
730 Excluded(ref x) => Excluded(x),
731 Unbounded => Unbounded,
732 }
733 }
734
735 #[inline]
737 #[unstable(feature = "bound_as_ref", issue = "80996")]
738 pub const fn as_mut(&mut self) -> Bound<&mut T> {
739 match *self {
740 Included(ref mut x) => Included(x),
741 Excluded(ref mut x) => Excluded(x),
742 Unbounded => Unbounded,
743 }
744 }
745
746 #[inline]
768 #[stable(feature = "bound_map", since = "1.77.0")]
769 pub fn map<U, F: FnOnce(T) -> U>(self, f: F) -> Bound<U> {
770 match self {
771 Unbounded => Unbounded,
772 Included(x) => Included(f(x)),
773 Excluded(x) => Excluded(f(x)),
774 }
775 }
776}
777
778impl<T: Copy> Bound<&T> {
779 #[unstable(feature = "bound_copied", issue = "145966")]
793 #[must_use]
794 pub const fn copied(self) -> Bound<T> {
795 match self {
796 Bound::Unbounded => Bound::Unbounded,
797 Bound::Included(x) => Bound::Included(*x),
798 Bound::Excluded(x) => Bound::Excluded(*x),
799 }
800 }
801}
802
803impl<T: Clone> Bound<&T> {
804 #[must_use = "`self` will be dropped if the result is not used"]
819 #[stable(feature = "bound_cloned", since = "1.55.0")]
820 #[rustc_const_unstable(feature = "const_range", issue = "none")]
821 pub const fn cloned(self) -> Bound<T>
822 where
823 T: [const] Clone,
824 {
825 match self {
826 Bound::Unbounded => Bound::Unbounded,
827 Bound::Included(x) => Bound::Included(x.clone()),
828 Bound::Excluded(x) => Bound::Excluded(x.clone()),
829 }
830 }
831}
832
833#[stable(feature = "collections_range", since = "1.28.0")]
836#[rustc_diagnostic_item = "RangeBounds"]
837#[rustc_const_unstable(feature = "const_range", issue = "none")]
838pub const trait RangeBounds<T: ?Sized> {
839 #[stable(feature = "collections_range", since = "1.28.0")]
853 fn start_bound(&self) -> Bound<&T>;
854
855 #[stable(feature = "collections_range", since = "1.28.0")]
869 fn end_bound(&self) -> Bound<&T>;
870
871 #[inline]
885 #[stable(feature = "range_contains", since = "1.35.0")]
886 fn contains<U>(&self, item: &U) -> bool
887 where
888 T: [const] PartialOrd<U>,
889 U: ?Sized + [const] PartialOrd<T>,
890 {
891 (match self.start_bound() {
892 Included(start) => start <= item,
893 Excluded(start) => start < item,
894 Unbounded => true,
895 }) && (match self.end_bound() {
896 Included(end) => item <= end,
897 Excluded(end) => item < end,
898 Unbounded => true,
899 })
900 }
901
902 #[unstable(feature = "range_bounds_is_empty", issue = "137300")]
954 fn is_empty(&self) -> bool
955 where
956 T: [const] PartialOrd,
957 {
958 !match (self.start_bound(), self.end_bound()) {
959 (Unbounded, _) | (_, Unbounded) => true,
960 (Included(start), Excluded(end))
961 | (Excluded(start), Included(end))
962 | (Excluded(start), Excluded(end)) => start < end,
963 (Included(start), Included(end)) => start <= end,
964 }
965 }
966}
967
968#[unstable(feature = "range_into_bounds", issue = "136903")]
974#[rustc_const_unstable(feature = "const_range", issue = "none")]
975pub const trait IntoBounds<T>: [const] RangeBounds<T> {
976 fn into_bounds(self) -> (Bound<T>, Bound<T>);
990
991 fn intersect<R>(self, other: R) -> (Bound<T>, Bound<T>)
1019 where
1020 Self: Sized,
1021 T: [const] Ord + [const] Destruct,
1022 R: Sized + [const] IntoBounds<T>,
1023 {
1024 let (self_start, self_end) = IntoBounds::into_bounds(self);
1025 let (other_start, other_end) = IntoBounds::into_bounds(other);
1026
1027 let start = match (self_start, other_start) {
1028 (Included(a), Included(b)) => Included(Ord::max(a, b)),
1029 (Excluded(a), Excluded(b)) => Excluded(Ord::max(a, b)),
1030 (Unbounded, Unbounded) => Unbounded,
1031
1032 (x, Unbounded) | (Unbounded, x) => x,
1033
1034 (Included(i), Excluded(e)) | (Excluded(e), Included(i)) => {
1035 if i > e {
1036 Included(i)
1037 } else {
1038 Excluded(e)
1039 }
1040 }
1041 };
1042 let end = match (self_end, other_end) {
1043 (Included(a), Included(b)) => Included(Ord::min(a, b)),
1044 (Excluded(a), Excluded(b)) => Excluded(Ord::min(a, b)),
1045 (Unbounded, Unbounded) => Unbounded,
1046
1047 (x, Unbounded) | (Unbounded, x) => x,
1048
1049 (Included(i), Excluded(e)) | (Excluded(e), Included(i)) => {
1050 if i < e {
1051 Included(i)
1052 } else {
1053 Excluded(e)
1054 }
1055 }
1056 };
1057
1058 (start, end)
1059 }
1060}
1061
1062use self::Bound::{Excluded, Included, Unbounded};
1063
1064#[stable(feature = "collections_range", since = "1.28.0")]
1065#[rustc_const_unstable(feature = "const_range", issue = "none")]
1066const impl<T: ?Sized> RangeBounds<T> for RangeFull {
1067 fn start_bound(&self) -> Bound<&T> {
1068 Unbounded
1069 }
1070 fn end_bound(&self) -> Bound<&T> {
1071 Unbounded
1072 }
1073}
1074
1075#[unstable(feature = "range_into_bounds", issue = "136903")]
1076#[rustc_const_unstable(feature = "const_range", issue = "none")]
1077const impl<T> IntoBounds<T> for RangeFull {
1078 fn into_bounds(self) -> (Bound<T>, Bound<T>) {
1079 (Unbounded, Unbounded)
1080 }
1081}
1082
1083#[stable(feature = "collections_range", since = "1.28.0")]
1084#[rustc_const_unstable(feature = "const_range", issue = "none")]
1085const impl<T> RangeBounds<T> for RangeFrom<T> {
1086 fn start_bound(&self) -> Bound<&T> {
1087 Included(&self.start)
1088 }
1089 fn end_bound(&self) -> Bound<&T> {
1090 Unbounded
1091 }
1092}
1093
1094#[unstable(feature = "range_into_bounds", issue = "136903")]
1095#[rustc_const_unstable(feature = "const_range", issue = "none")]
1096const impl<T> IntoBounds<T> for RangeFrom<T> {
1097 fn into_bounds(self) -> (Bound<T>, Bound<T>) {
1098 (Included(self.start), Unbounded)
1099 }
1100}
1101
1102#[stable(feature = "collections_range", since = "1.28.0")]
1103#[rustc_const_unstable(feature = "const_range", issue = "none")]
1104const impl<T> RangeBounds<T> for RangeTo<T> {
1105 fn start_bound(&self) -> Bound<&T> {
1106 Unbounded
1107 }
1108 fn end_bound(&self) -> Bound<&T> {
1109 Excluded(&self.end)
1110 }
1111}
1112
1113#[unstable(feature = "range_into_bounds", issue = "136903")]
1114#[rustc_const_unstable(feature = "const_range", issue = "none")]
1115const impl<T> IntoBounds<T> for RangeTo<T> {
1116 fn into_bounds(self) -> (Bound<T>, Bound<T>) {
1117 (Unbounded, Excluded(self.end))
1118 }
1119}
1120
1121#[stable(feature = "collections_range", since = "1.28.0")]
1122#[rustc_const_unstable(feature = "const_range", issue = "none")]
1123const impl<T> RangeBounds<T> for Range<T> {
1124 fn start_bound(&self) -> Bound<&T> {
1125 Included(&self.start)
1126 }
1127 fn end_bound(&self) -> Bound<&T> {
1128 Excluded(&self.end)
1129 }
1130}
1131
1132#[unstable(feature = "range_into_bounds", issue = "136903")]
1133#[rustc_const_unstable(feature = "const_range", issue = "none")]
1134const impl<T> IntoBounds<T> for Range<T> {
1135 fn into_bounds(self) -> (Bound<T>, Bound<T>) {
1136 (Included(self.start), Excluded(self.end))
1137 }
1138}
1139
1140#[stable(feature = "collections_range", since = "1.28.0")]
1141#[rustc_const_unstable(feature = "const_range", issue = "none")]
1142const impl<T> RangeBounds<T> for RangeInclusive<T> {
1143 fn start_bound(&self) -> Bound<&T> {
1144 Included(&self.start)
1145 }
1146 fn end_bound(&self) -> Bound<&T> {
1147 if self.exhausted {
1148 Excluded(&self.start)
1153 } else {
1154 Included(&self.end)
1155 }
1156 }
1157}
1158
1159#[unstable(feature = "range_into_bounds", issue = "136903")]
1160#[rustc_const_unstable(feature = "const_range", issue = "none")]
1161const impl<T> IntoBounds<T> for RangeInclusive<T> {
1162 fn into_bounds(self) -> (Bound<T>, Bound<T>) {
1163 if !!self.exhausted {
{
crate::panicking::panic_fmt(format_args!("attempted to convert from an exhausted `RangeInclusive` (unspecified behavior)"));
}
};assert!(
1164 !self.exhausted,
1165 "attempted to convert from an exhausted `RangeInclusive` (unspecified behavior)"
1166 );
1167
1168 (Included(self.start), Included(self.end))
1169 }
1170}
1171
1172#[stable(feature = "collections_range", since = "1.28.0")]
1173#[rustc_const_unstable(feature = "const_range", issue = "none")]
1174const impl<T> RangeBounds<T> for RangeToInclusive<T> {
1175 fn start_bound(&self) -> Bound<&T> {
1176 Unbounded
1177 }
1178 fn end_bound(&self) -> Bound<&T> {
1179 Included(&self.end)
1180 }
1181}
1182
1183#[unstable(feature = "range_into_bounds", issue = "136903")]
1184#[rustc_const_unstable(feature = "const_range", issue = "none")]
1185const impl<T> IntoBounds<T> for RangeToInclusive<T> {
1186 fn into_bounds(self) -> (Bound<T>, Bound<T>) {
1187 (Unbounded, Included(self.end))
1188 }
1189}
1190
1191#[stable(feature = "collections_range", since = "1.28.0")]
1192#[rustc_const_unstable(feature = "const_range", issue = "none")]
1193const impl<T> RangeBounds<T> for (Bound<T>, Bound<T>) {
1194 fn start_bound(&self) -> Bound<&T> {
1195 match *self {
1196 (Included(ref start), _) => Included(start),
1197 (Excluded(ref start), _) => Excluded(start),
1198 (Unbounded, _) => Unbounded,
1199 }
1200 }
1201
1202 fn end_bound(&self) -> Bound<&T> {
1203 match *self {
1204 (_, Included(ref end)) => Included(end),
1205 (_, Excluded(ref end)) => Excluded(end),
1206 (_, Unbounded) => Unbounded,
1207 }
1208 }
1209}
1210
1211#[unstable(feature = "range_into_bounds", issue = "136903")]
1212#[rustc_const_unstable(feature = "const_range", issue = "none")]
1213const impl<T> IntoBounds<T> for (Bound<T>, Bound<T>) {
1214 fn into_bounds(self) -> (Bound<T>, Bound<T>) {
1215 self
1216 }
1217}
1218
1219#[stable(feature = "collections_range", since = "1.28.0")]
1220#[rustc_const_unstable(feature = "const_range", issue = "none")]
1221const impl<'a, T: ?Sized + 'a> RangeBounds<T> for (Bound<&'a T>, Bound<&'a T>) {
1222 fn start_bound(&self) -> Bound<&T> {
1223 self.0
1224 }
1225
1226 fn end_bound(&self) -> Bound<&T> {
1227 self.1
1228 }
1229}
1230
1231#[stable(feature = "collections_range", since = "1.28.0")]
1238#[rustc_const_unstable(feature = "const_range", issue = "none")]
1239const impl<T> RangeBounds<T> for RangeFrom<&T> {
1240 fn start_bound(&self) -> Bound<&T> {
1241 Included(self.start)
1242 }
1243 fn end_bound(&self) -> Bound<&T> {
1244 Unbounded
1245 }
1246}
1247
1248#[stable(feature = "collections_range", since = "1.28.0")]
1255#[rustc_const_unstable(feature = "const_range", issue = "none")]
1256const impl<T> RangeBounds<T> for RangeTo<&T> {
1257 fn start_bound(&self) -> Bound<&T> {
1258 Unbounded
1259 }
1260 fn end_bound(&self) -> Bound<&T> {
1261 Excluded(self.end)
1262 }
1263}
1264
1265#[stable(feature = "collections_range", since = "1.28.0")]
1272#[rustc_const_unstable(feature = "const_range", issue = "none")]
1273const impl<T> RangeBounds<T> for Range<&T> {
1274 fn start_bound(&self) -> Bound<&T> {
1275 Included(self.start)
1276 }
1277 fn end_bound(&self) -> Bound<&T> {
1278 Excluded(self.end)
1279 }
1280}
1281
1282#[stable(feature = "collections_range", since = "1.28.0")]
1289#[rustc_const_unstable(feature = "const_range", issue = "none")]
1290const impl<T> RangeBounds<T> for RangeInclusive<&T> {
1291 fn start_bound(&self) -> Bound<&T> {
1292 Included(self.start)
1293 }
1294 fn end_bound(&self) -> Bound<&T> {
1295 Included(self.end)
1296 }
1297}
1298
1299#[stable(feature = "collections_range", since = "1.28.0")]
1306#[rustc_const_unstable(feature = "const_range", issue = "none")]
1307const impl<T> RangeBounds<T> for RangeToInclusive<&T> {
1308 fn start_bound(&self) -> Bound<&T> {
1309 Unbounded
1310 }
1311 fn end_bound(&self) -> Bound<&T> {
1312 Included(self.end)
1313 }
1314}
1315
1316#[unstable(feature = "one_sided_range", issue = "69780")]
1319#[allow(missing_debug_implementations)]
1320pub enum OneSidedRangeBound {
1321 StartInclusive,
1323 End,
1325 EndInclusive,
1327}
1328
1329#[unstable(feature = "one_sided_range", issue = "69780")]
1336#[rustc_const_unstable(feature = "const_range", issue = "none")]
1337pub const trait OneSidedRange<T>: RangeBounds<T> {
1338 fn bound(self) -> (OneSidedRangeBound, T);
1341}
1342
1343#[unstable(feature = "one_sided_range", issue = "69780")]
1344#[rustc_const_unstable(feature = "const_range", issue = "none")]
1345const impl<T> OneSidedRange<T> for RangeTo<T>
1346where
1347 Self: RangeBounds<T>,
1348{
1349 fn bound(self) -> (OneSidedRangeBound, T) {
1350 (OneSidedRangeBound::End, self.end)
1351 }
1352}
1353
1354#[unstable(feature = "one_sided_range", issue = "69780")]
1355#[rustc_const_unstable(feature = "const_range", issue = "none")]
1356const impl<T> OneSidedRange<T> for RangeFrom<T>
1357where
1358 Self: RangeBounds<T>,
1359{
1360 fn bound(self) -> (OneSidedRangeBound, T) {
1361 (OneSidedRangeBound::StartInclusive, self.start)
1362 }
1363}
1364
1365#[unstable(feature = "one_sided_range", issue = "69780")]
1366#[rustc_const_unstable(feature = "const_range", issue = "none")]
1367const impl<T> OneSidedRange<T> for RangeToInclusive<T>
1368where
1369 Self: RangeBounds<T>,
1370{
1371 fn bound(self) -> (OneSidedRangeBound, T) {
1372 (OneSidedRangeBound::EndInclusive, self.end)
1373 }
1374}