1use crate::fmt;
19use crate::hash::Hash;
20use crate::marker::Destruct;
21
22mod iter;
23
24#[stable(feature = "new_range_api_legacy", since = "1.98.0")]
25pub mod legacy;
26
27use core::ops::Bound::{self, Excluded, Included, Unbounded};
28
29#[doc(inline)]
30#[stable(feature = "new_range_from_api", since = "1.96.0")]
31pub use iter::RangeFromIter;
32#[doc(inline)]
33#[stable(feature = "new_range_inclusive_api", since = "1.95.0")]
34pub use iter::RangeInclusiveIter;
35#[doc(inline)]
36#[stable(feature = "new_range_api", since = "1.96.0")]
37pub use iter::RangeIter;
38
39use crate::iter::Step;
40use crate::ops::{IntoBounds, OneSidedRange, OneSidedRangeBound, RangeBounds};
43#[doc(inline)]
44#[stable(feature = "new_range_api_exports", since = "1.98.0")]
45pub use crate::ops::{RangeFull, RangeTo};
46
47#[lang = "RangeCopy"]
70#[derive(#[automatically_derived]#[stable(feature = "new_range_api", since = "1.96.0")]
impl<Idx: crate::marker::Copy> crate::marker::Copy for Range<Idx> { }Copy, #[automatically_derived]#[stable(feature = "new_range_api", since = "1.96.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)]
71#[derive_const(#[automatically_derived]#[rustc_const_unstable(feature = "derive_const", issue = "143874")]#[stable(feature
= "new_range_api", since = "1.96.0")]
const impl<Idx: [const] crate::clone::Clone> crate::clone::Clone for
Range<Idx> {
#[inline]
#[rustc_trivial_field_reads]
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 = "143874")]#[stable(feature
= "new_range_api", since = "1.96.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 = "new_range_api", since = "1.96.0")]
impl<Idx: crate::cmp::PartialEq> crate::marker::StructuralPartialEq for
Range<Idx> {
}
#[automatically_derived]#[rustc_const_unstable(feature = "derive_const", issue = "143874")]#[stable(feature
= "new_range_api", since = "1.96.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, #[automatically_derived]#[rustc_const_unstable(feature = "derive_const", issue = "143874")]#[stable(feature
= "new_range_api", since = "1.96.0")]
const impl<Idx: [const] 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)]
72#[stable(feature = "new_range_api", since = "1.96.0")]
73pub struct Range<Idx> {
74 #[stable(feature = "new_range_api", since = "1.96.0")]
76 pub start: Idx,
77 #[stable(feature = "new_range_api", since = "1.96.0")]
79 pub end: Idx,
80}
81
82#[stable(feature = "new_range_api", since = "1.96.0")]
83impl<Idx: fmt::Debug> fmt::Debug for Range<Idx> {
84 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
85 self.start.fmt(fmt)?;
86 fmt.write_fmt(format_args!(".."))write!(fmt, "..")?;
87 self.end.fmt(fmt)?;
88 Ok(())
89 }
90}
91
92impl<Idx: Step> Range<Idx> {
93 #[stable(feature = "new_range_api", since = "1.96.0")]
108 #[inline]
109 pub fn iter(&self) -> RangeIter<Idx> {
110 self.clone().into_iter()
111 }
112}
113
114impl<Idx: PartialOrd<Idx>> Range<Idx> {
115 #[inline]
136 #[stable(feature = "new_range_api", since = "1.96.0")]
137 #[rustc_const_unstable(feature = "const_range", issue = "none")]
138 pub const fn contains<U>(&self, item: &U) -> bool
139 where
140 Idx: [const] PartialOrd<U>,
141 U: ?Sized + [const] PartialOrd<Idx>,
142 {
143 <Self as RangeBounds<Idx>>::contains(self, item)
144 }
145
146 #[inline]
168 #[stable(feature = "new_range_api", since = "1.96.0")]
169 #[rustc_const_unstable(feature = "const_range", issue = "none")]
170 #[expect(clippy::neg_cmp_op_on_partial_ord, reason = "incomparable ranges are empty")]
171 pub const fn is_empty(&self) -> bool
172 where
173 Idx: [const] PartialOrd,
174 {
175 !(self.start < self.end)
176 }
177}
178
179#[stable(feature = "new_range_api", since = "1.96.0")]
180#[rustc_const_unstable(feature = "const_range", issue = "none")]
181const impl<T> RangeBounds<T> for Range<T> {
182 fn start_bound(&self) -> Bound<&T> {
183 Included(&self.start)
184 }
185 fn end_bound(&self) -> Bound<&T> {
186 Excluded(&self.end)
187 }
188}
189
190#[stable(feature = "new_range_api", since = "1.96.0")]
197#[rustc_const_unstable(feature = "const_range", issue = "none")]
198const impl<T> RangeBounds<T> for Range<&T> {
199 fn start_bound(&self) -> Bound<&T> {
200 Included(self.start)
201 }
202 fn end_bound(&self) -> Bound<&T> {
203 Excluded(self.end)
204 }
205}
206
207#[unstable(feature = "range_into_bounds", issue = "136903")]
208#[rustc_const_unstable(feature = "const_range", issue = "none")]
209const impl<T> IntoBounds<T> for Range<T> {
210 fn into_bounds(self) -> (Bound<T>, Bound<T>) {
211 (Included(self.start), Excluded(self.end))
212 }
213}
214
215#[stable(feature = "new_range_api", since = "1.96.0")]
216#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
217const impl<T> From<Range<T>> for legacy::Range<T> {
218 #[inline]
219 fn from(value: Range<T>) -> Self {
220 Self { start: value.start, end: value.end }
221 }
222}
223#[stable(feature = "new_range_api", since = "1.96.0")]
224#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
225const impl<T> From<legacy::Range<T>> for Range<T> {
226 #[inline]
227 fn from(value: legacy::Range<T>) -> Self {
228 Self { start: value.start, end: value.end }
229 }
230}
231
232#[lang = "RangeInclusiveCopy"]
251#[derive(#[automatically_derived]#[stable(feature = "new_range_inclusive_api", since =
"1.95.0")]
impl<Idx: crate::clone::Clone> crate::clone::Clone for RangeInclusive<Idx> {
#[inline]
#[rustc_trivial_field_reads]
fn clone(&self) -> Self {
RangeInclusive {
start: crate::clone::Clone::clone(&self.start),
last: crate::clone::Clone::clone(&self.last),
}
}
}Clone, #[automatically_derived]#[stable(feature = "new_range_inclusive_api", since =
"1.95.0")]
impl<Idx: crate::marker::Copy> crate::marker::Copy for RangeInclusive<Idx> { }Copy, #[automatically_derived]#[stable(feature = "new_range_inclusive_api", since =
"1.95.0")]
impl<Idx: crate::cmp::PartialEq> crate::marker::StructuralPartialEq for
RangeInclusive<Idx> {
}
#[automatically_derived]#[stable(feature = "new_range_inclusive_api", since =
"1.95.0")]
impl<Idx: crate::cmp::PartialEq> crate::cmp::PartialEq for RangeInclusive<Idx>
{
#[inline]
fn eq(&self, other: &Self) -> bool {
self.start == other.start && self.last == other.last
}
}PartialEq, #[automatically_derived]#[stable(feature = "new_range_inclusive_api", since =
"1.95.0")]
impl<Idx: 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>;
}
}Eq, #[automatically_derived]#[stable(feature = "new_range_inclusive_api", since =
"1.95.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.last, state)
}
}Hash)]
252#[stable(feature = "new_range_inclusive_api", since = "1.95.0")]
253pub struct RangeInclusive<Idx> {
254 #[stable(feature = "new_range_inclusive_api", since = "1.95.0")]
256 pub start: Idx,
257 #[stable(feature = "new_range_inclusive_api", since = "1.95.0")]
259 pub last: Idx,
260}
261
262#[stable(feature = "new_range_inclusive_api", since = "1.95.0")]
263impl<Idx: fmt::Debug> fmt::Debug for RangeInclusive<Idx> {
264 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
265 self.start.fmt(fmt)?;
266 fmt.write_fmt(format_args!("..="))write!(fmt, "..=")?;
267 self.last.fmt(fmt)?;
268 Ok(())
269 }
270}
271
272impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> {
273 #[inline]
295 #[stable(feature = "new_range_inclusive_api", since = "1.95.0")]
296 #[rustc_const_unstable(feature = "const_range", issue = "none")]
297 pub const fn contains<U>(&self, item: &U) -> bool
298 where
299 Idx: [const] PartialOrd<U>,
300 U: ?Sized + [const] PartialOrd<Idx>,
301 {
302 <Self as RangeBounds<Idx>>::contains(self, item)
303 }
304
305 #[stable(feature = "new_range_inclusive_api", since = "1.95.0")]
327 #[inline]
328 #[rustc_const_unstable(feature = "const_range", issue = "none")]
329 #[expect(clippy::neg_cmp_op_on_partial_ord, reason = "incomparable ranges are empty")]
330 pub const fn is_empty(&self) -> bool
331 where
332 Idx: [const] PartialOrd,
333 {
334 !(self.start <= self.last)
335 }
336}
337
338impl<Idx: Step> RangeInclusive<Idx> {
339 #[stable(feature = "new_range_inclusive_api", since = "1.95.0")]
354 #[inline]
355 pub fn iter(&self) -> RangeInclusiveIter<Idx> {
356 self.clone().into_iter()
357 }
358}
359
360#[stable(feature = "new_range_inclusive_api", since = "1.95.0")]
361#[rustc_const_unstable(feature = "const_range", issue = "none")]
362const impl<T> RangeBounds<T> for RangeInclusive<T> {
363 fn start_bound(&self) -> Bound<&T> {
364 Included(&self.start)
365 }
366 fn end_bound(&self) -> Bound<&T> {
367 Included(&self.last)
368 }
369}
370
371#[stable(feature = "new_range_inclusive_api", since = "1.95.0")]
378#[rustc_const_unstable(feature = "const_range", issue = "none")]
379const impl<T> RangeBounds<T> for RangeInclusive<&T> {
380 fn start_bound(&self) -> Bound<&T> {
381 Included(self.start)
382 }
383 fn end_bound(&self) -> Bound<&T> {
384 Included(self.last)
385 }
386}
387
388#[unstable(feature = "range_into_bounds", issue = "136903")]
390#[rustc_const_unstable(feature = "const_range", issue = "none")]
391const impl<T> IntoBounds<T> for RangeInclusive<T> {
392 fn into_bounds(self) -> (Bound<T>, Bound<T>) {
393 (Included(self.start), Included(self.last))
394 }
395}
396
397#[stable(feature = "new_range_inclusive_api", since = "1.95.0")]
398#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
399const impl<T> From<RangeInclusive<T>> for legacy::RangeInclusive<T> {
400 #[inline]
401 fn from(value: RangeInclusive<T>) -> Self {
402 Self::new(value.start, value.last)
403 }
404}
405#[stable(feature = "new_range_inclusive_api", since = "1.95.0")]
406#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
407const impl<T> From<legacy::RangeInclusive<T>> for RangeInclusive<T> {
408 #[inline]
449 fn from(value: legacy::RangeInclusive<T>) -> Self {
450 if !!value.exhausted {
{
crate::panicking::panic_fmt(format_args!("attempted to convert from an exhausted `legacy::RangeInclusive`"));
}
};assert!(
451 !value.exhausted,
452 "attempted to convert from an exhausted `legacy::RangeInclusive`"
453 );
454
455 let (start, last) = value.into_inner();
456 RangeInclusive { start, last }
457 }
458}
459
460#[lang = "RangeFromCopy"]
488#[derive(#[automatically_derived]#[stable(feature = "new_range_from_api", since =
"1.96.0")]
impl<Idx: crate::marker::Copy> crate::marker::Copy for RangeFrom<Idx> { }Copy, #[automatically_derived]#[stable(feature = "new_range_from_api", since =
"1.96.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)]
489#[derive_const(#[automatically_derived]#[rustc_const_unstable(feature = "derive_const", issue = "143874")]#[stable(feature
= "new_range_from_api", since = "1.96.0")]
const impl<Idx: [const] crate::clone::Clone> crate::clone::Clone for
RangeFrom<Idx> {
#[inline]
#[rustc_trivial_field_reads]
fn clone(&self) -> Self {
RangeFrom { start: crate::clone::Clone::clone(&self.start) }
}
}Clone, #[automatically_derived]#[stable(feature = "new_range_from_api", since =
"1.96.0")]
impl<Idx: crate::cmp::PartialEq> crate::marker::StructuralPartialEq for
RangeFrom<Idx> {
}
#[automatically_derived]#[rustc_const_unstable(feature = "derive_const", issue = "143874")]#[stable(feature
= "new_range_from_api", since = "1.96.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, #[automatically_derived]#[rustc_const_unstable(feature = "derive_const", issue = "143874")]#[stable(feature
= "new_range_from_api", since = "1.96.0")]
const impl<Idx: [const] 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)]
490#[stable(feature = "new_range_from_api", since = "1.96.0")]
491pub struct RangeFrom<Idx> {
492 #[stable(feature = "new_range_from_api", since = "1.96.0")]
494 pub start: Idx,
495}
496
497#[stable(feature = "new_range_from_api", since = "1.96.0")]
498impl<Idx: fmt::Debug> fmt::Debug for RangeFrom<Idx> {
499 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
500 self.start.fmt(fmt)?;
501 fmt.write_fmt(format_args!(".."))write!(fmt, "..")?;
502 Ok(())
503 }
504}
505
506impl<Idx: Step> RangeFrom<Idx> {
507 #[stable(feature = "new_range_from_api", since = "1.96.0")]
522 #[inline]
523 pub fn iter(&self) -> RangeFromIter<Idx> {
524 self.clone().into_iter()
525 }
526}
527
528impl<Idx: PartialOrd<Idx>> RangeFrom<Idx> {
529 #[inline]
545 #[stable(feature = "new_range_from_api", since = "1.96.0")]
546 #[rustc_const_unstable(feature = "const_range", issue = "none")]
547 pub const fn contains<U>(&self, item: &U) -> bool
548 where
549 Idx: [const] PartialOrd<U>,
550 U: ?Sized + [const] PartialOrd<Idx>,
551 {
552 <Self as RangeBounds<Idx>>::contains(self, item)
553 }
554}
555
556#[stable(feature = "new_range_from_api", since = "1.96.0")]
557#[rustc_const_unstable(feature = "const_range", issue = "none")]
558const impl<T> RangeBounds<T> for RangeFrom<T> {
559 fn start_bound(&self) -> Bound<&T> {
560 Included(&self.start)
561 }
562 fn end_bound(&self) -> Bound<&T> {
563 Unbounded
564 }
565}
566
567#[stable(feature = "new_range_from_api", since = "1.96.0")]
574#[rustc_const_unstable(feature = "const_range", issue = "none")]
575const impl<T> RangeBounds<T> for RangeFrom<&T> {
576 fn start_bound(&self) -> Bound<&T> {
577 Included(self.start)
578 }
579 fn end_bound(&self) -> Bound<&T> {
580 Unbounded
581 }
582}
583
584#[unstable(feature = "range_into_bounds", issue = "136903")]
585#[rustc_const_unstable(feature = "const_range", issue = "none")]
586const impl<T> IntoBounds<T> for RangeFrom<T> {
587 fn into_bounds(self) -> (Bound<T>, Bound<T>) {
588 (Included(self.start), Unbounded)
589 }
590}
591
592#[unstable(feature = "one_sided_range", issue = "69780")]
593#[rustc_const_unstable(feature = "const_range", issue = "none")]
594const impl<T> OneSidedRange<T> for RangeFrom<T>
595where
596 Self: RangeBounds<T>,
597{
598 fn bound(self) -> (OneSidedRangeBound, T) {
599 (OneSidedRangeBound::StartInclusive, self.start)
600 }
601}
602
603#[stable(feature = "new_range_from_api", since = "1.96.0")]
604#[rustc_const_unstable(feature = "const_index", issue = "143775")]
605const impl<T> From<RangeFrom<T>> for legacy::RangeFrom<T> {
606 #[inline]
607 fn from(value: RangeFrom<T>) -> Self {
608 Self { start: value.start }
609 }
610}
611#[stable(feature = "new_range_from_api", since = "1.96.0")]
612#[rustc_const_unstable(feature = "const_index", issue = "143775")]
613const impl<T> From<legacy::RangeFrom<T>> for RangeFrom<T> {
614 #[inline]
615 fn from(value: legacy::RangeFrom<T>) -> Self {
616 Self { start: value.start }
617 }
618}
619
620#[lang = "RangeToInclusiveCopy"]
663#[doc(alias = "..=")]
664#[derive(#[automatically_derived]#[stable(feature = "new_range_to_inclusive_api", since
= "1.96.0")]
impl<Idx: crate::marker::Copy> crate::marker::Copy for RangeToInclusive<Idx> {
}Copy, #[automatically_derived]#[stable(feature = "new_range_to_inclusive_api", since
= "1.96.0")]
impl<Idx: crate::clone::Clone> crate::clone::Clone for RangeToInclusive<Idx> {
#[inline]
#[rustc_trivial_field_reads]
fn clone(&self) -> Self {
RangeToInclusive { last: crate::clone::Clone::clone(&self.last) }
}
}Clone, #[automatically_derived]#[stable(feature = "new_range_to_inclusive_api", since
= "1.96.0")]
impl<Idx: crate::cmp::PartialEq> crate::marker::StructuralPartialEq for
RangeToInclusive<Idx> {
}
#[automatically_derived]#[stable(feature = "new_range_to_inclusive_api", since
= "1.96.0")]
impl<Idx: crate::cmp::PartialEq> crate::cmp::PartialEq for
RangeToInclusive<Idx> {
#[inline]
fn eq(&self, other: &Self) -> bool { self.last == other.last }
}PartialEq, #[automatically_derived]#[stable(feature = "new_range_to_inclusive_api", since
= "1.96.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, #[automatically_derived]#[stable(feature = "new_range_to_inclusive_api", since
= "1.96.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.last, state)
}
}Hash)]
665#[stable(feature = "new_range_to_inclusive_api", since = "1.96.0")]
666pub struct RangeToInclusive<Idx> {
667 #[stable(feature = "new_range_to_inclusive_api", since = "1.96.0")]
669 pub last: Idx,
670}
671
672#[stable(feature = "new_range_to_inclusive_api", since = "1.96.0")]
673impl<Idx: fmt::Debug> fmt::Debug for RangeToInclusive<Idx> {
674 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
675 fmt.write_fmt(format_args!("..="))write!(fmt, "..=")?;
676 self.last.fmt(fmt)?;
677 Ok(())
678 }
679}
680
681impl<Idx: PartialOrd<Idx>> RangeToInclusive<Idx> {
682 #[inline]
696 #[stable(feature = "new_range_to_inclusive_api", since = "1.96.0")]
697 #[rustc_const_unstable(feature = "const_range", issue = "none")]
698 pub const fn contains<U>(&self, item: &U) -> bool
699 where
700 Idx: [const] PartialOrd<U>,
701 U: ?Sized + [const] PartialOrd<Idx>,
702 {
703 <Self as RangeBounds<Idx>>::contains(self, item)
704 }
705}
706
707#[stable(feature = "new_range_to_inclusive_api", since = "1.96.0")]
708impl<T> From<legacy::RangeToInclusive<T>> for RangeToInclusive<T> {
709 fn from(value: legacy::RangeToInclusive<T>) -> Self {
710 Self { last: value.end }
711 }
712}
713#[stable(feature = "new_range_to_inclusive_api", since = "1.96.0")]
714impl<T> From<RangeToInclusive<T>> for legacy::RangeToInclusive<T> {
715 fn from(value: RangeToInclusive<T>) -> Self {
716 Self { end: value.last }
717 }
718}
719
720#[stable(feature = "new_range_to_inclusive_api", since = "1.96.0")]
724#[rustc_const_unstable(feature = "const_range", issue = "none")]
725const impl<T> RangeBounds<T> for RangeToInclusive<T> {
726 fn start_bound(&self) -> Bound<&T> {
727 Unbounded
728 }
729 fn end_bound(&self) -> Bound<&T> {
730 Included(&self.last)
731 }
732}
733
734#[stable(feature = "new_range_to_inclusive_api", since = "1.96.0")]
735#[rustc_const_unstable(feature = "const_range", issue = "none")]
736const impl<T> RangeBounds<T> for RangeToInclusive<&T> {
737 fn start_bound(&self) -> Bound<&T> {
738 Unbounded
739 }
740 fn end_bound(&self) -> Bound<&T> {
741 Included(self.last)
742 }
743}
744
745#[unstable(feature = "range_into_bounds", issue = "136903")]
746#[rustc_const_unstable(feature = "const_range", issue = "none")]
747const impl<T> IntoBounds<T> for RangeToInclusive<T> {
748 fn into_bounds(self) -> (Bound<T>, Bound<T>) {
749 (Unbounded, Included(self.last))
750 }
751}
752
753#[unstable(feature = "one_sided_range", issue = "69780")]
754#[rustc_const_unstable(feature = "const_range", issue = "none")]
755const impl<T> OneSidedRange<T> for RangeToInclusive<T>
756where
757 Self: RangeBounds<T>,
758{
759 fn bound(self) -> (OneSidedRangeBound, T) {
760 (OneSidedRangeBound::EndInclusive, self.last)
761 }
762}
763
764#[unstable(feature = "range_intersect", issue = "165428")]
803#[rustc_const_unstable(feature = "const_range", issue = "none")]
804pub const trait RangeIntersect<Rhs = Self>: Sized {
805 #[unstable(feature = "range_intersect", issue = "165428")]
807 type Intersection;
808
809 #[unstable(feature = "range_intersect", issue = "165428")]
811 #[must_use]
812 fn intersect(self, rhs: Rhs) -> Self::Intersection;
813}
814
815#[unstable(feature = "range_intersect", issue = "165428")]
816#[rustc_const_unstable(feature = "const_range", issue = "none")]
817const impl<T> RangeIntersect for (Bound<T>, Bound<T>)
818where
819 T: [const] Ord + [const] Destruct,
820{
821 type Intersection = Self;
822
823 #[inline]
824 fn intersect(self, rhs: Self) -> Self::Intersection {
825 let (self_start, self_end) = self;
826 let (other_start, other_end) = rhs;
827
828 let start = match (self_start, other_start) {
829 (Included(a), Included(b)) => Included(Ord::max(a, b)),
830 (Excluded(a), Excluded(b)) => Excluded(Ord::max(a, b)),
831 (Unbounded, Unbounded) => Unbounded,
832
833 (x, Unbounded) | (Unbounded, x) => x,
834
835 (Included(i), Excluded(e)) | (Excluded(e), Included(i)) => {
836 if i > e {
837 Included(i)
838 } else {
839 Excluded(e)
840 }
841 }
842 };
843 let end = match (self_end, other_end) {
844 (Included(a), Included(b)) => Included(Ord::min(a, b)),
845 (Excluded(a), Excluded(b)) => Excluded(Ord::min(a, b)),
846 (Unbounded, Unbounded) => Unbounded,
847
848 (x, Unbounded) | (Unbounded, x) => x,
849
850 (Included(i), Excluded(e)) | (Excluded(e), Included(i)) => {
851 if i < e {
852 Included(i)
853 } else {
854 Excluded(e)
855 }
856 }
857 };
858
859 (start, end)
860 }
861}
862
863#[unstable(feature = "range_intersect", issue = "165428")]
864#[rustc_const_unstable(feature = "const_range", issue = "none")]
865const impl<T> RangeIntersect for Range<T>
866where
867 T: [const] Ord + [const] Destruct,
868{
869 type Intersection = Range<T>;
870
871 #[inline]
872 fn intersect(self, rhs: Self) -> Self::Intersection {
873 Range { start: Ord::max(self.start, rhs.start), end: Ord::min(self.end, rhs.end) }
874 }
875}
876
877#[unstable(feature = "range_intersect", issue = "165428")]
878#[rustc_const_unstable(feature = "const_range", issue = "none")]
879const impl<T> RangeIntersect<RangeFrom<T>> for Range<T>
880where
881 T: [const] Ord + [const] Destruct,
882{
883 type Intersection = Range<T>;
884
885 #[inline]
886 fn intersect(self, rhs: RangeFrom<T>) -> Self::Intersection {
887 Range { start: Ord::max(self.start, rhs.start), end: self.end }
888 }
889}
890
891#[unstable(feature = "range_intersect", issue = "165428")]
892#[rustc_const_unstable(feature = "const_range", issue = "none")]
893const impl<T> RangeIntersect<RangeTo<T>> for Range<T>
894where
895 T: [const] Ord + [const] Destruct,
896{
897 type Intersection = Range<T>;
898
899 #[inline]
900 fn intersect(self, rhs: RangeTo<T>) -> Self::Intersection {
901 Range { start: self.start, end: Ord::min(self.end, rhs.end) }
902 }
903}
904
905#[unstable(feature = "range_intersect", issue = "165428")]
906#[rustc_const_unstable(feature = "const_range", issue = "none")]
907const impl<T> RangeIntersect<RangeFull> for Range<T> {
908 type Intersection = Range<T>;
909
910 #[inline]
911 fn intersect(self, _rhs: RangeFull) -> Self::Intersection {
912 self
913 }
914}
915
916#[unstable(feature = "range_intersect", issue = "165428")]
917#[rustc_const_unstable(feature = "const_range", issue = "none")]
918const impl<T> RangeIntersect<Range<T>> for RangeFrom<T>
919where
920 T: [const] Ord + [const] Destruct,
921{
922 type Intersection = Range<T>;
923
924 #[inline]
925 fn intersect(self, rhs: Range<T>) -> Self::Intersection {
926 Range { start: Ord::max(self.start, rhs.start), end: rhs.end }
927 }
928}
929
930#[unstable(feature = "range_intersect", issue = "165428")]
931#[rustc_const_unstable(feature = "const_range", issue = "none")]
932const impl<T> RangeIntersect for RangeFrom<T>
933where
934 T: [const] Ord + [const] Destruct,
935{
936 type Intersection = RangeFrom<T>;
937
938 #[inline]
939 fn intersect(self, rhs: Self) -> Self::Intersection {
940 RangeFrom { start: Ord::max(self.start, rhs.start) }
941 }
942}
943
944#[unstable(feature = "range_intersect", issue = "165428")]
945#[rustc_const_unstable(feature = "const_range", issue = "none")]
946const impl<T> RangeIntersect<RangeTo<T>> for RangeFrom<T> {
947 type Intersection = Range<T>;
948
949 #[inline]
950 fn intersect(self, rhs: RangeTo<T>) -> Self::Intersection {
951 Range { start: self.start, end: rhs.end }
952 }
953}
954
955#[unstable(feature = "range_intersect", issue = "165428")]
956#[rustc_const_unstable(feature = "const_range", issue = "none")]
957const impl<T> RangeIntersect<RangeFull> for RangeFrom<T> {
958 type Intersection = RangeFrom<T>;
959
960 #[inline]
961 fn intersect(self, _rhs: RangeFull) -> Self::Intersection {
962 self
963 }
964}
965
966#[unstable(feature = "range_intersect", issue = "165428")]
967#[rustc_const_unstable(feature = "const_range", issue = "none")]
968const impl<T> RangeIntersect<RangeInclusive<T>> for RangeFrom<T>
969where
970 T: [const] Ord + [const] Destruct,
971{
972 type Intersection = RangeInclusive<T>;
973
974 #[inline]
975 fn intersect(self, rhs: RangeInclusive<T>) -> Self::Intersection {
976 RangeInclusive { start: Ord::max(self.start, rhs.start), last: rhs.last }
977 }
978}
979
980#[unstable(feature = "range_intersect", issue = "165428")]
981#[rustc_const_unstable(feature = "const_range", issue = "none")]
982const impl<T> RangeIntersect<RangeToInclusive<T>> for RangeFrom<T> {
983 type Intersection = RangeInclusive<T>;
984
985 #[inline]
986 fn intersect(self, rhs: RangeToInclusive<T>) -> Self::Intersection {
987 RangeInclusive { start: self.start, last: rhs.last }
988 }
989}
990
991#[unstable(feature = "range_intersect", issue = "165428")]
992#[rustc_const_unstable(feature = "const_range", issue = "none")]
993const impl<T> RangeIntersect<Range<T>> for RangeTo<T>
994where
995 T: [const] Ord + [const] Destruct,
996{
997 type Intersection = Range<T>;
998
999 #[inline]
1000 fn intersect(self, rhs: Range<T>) -> Self::Intersection {
1001 Range { start: rhs.start, end: Ord::min(self.end, rhs.end) }
1002 }
1003}
1004
1005#[unstable(feature = "range_intersect", issue = "165428")]
1006#[rustc_const_unstable(feature = "const_range", issue = "none")]
1007const impl<T> RangeIntersect<RangeFrom<T>> for RangeTo<T> {
1008 type Intersection = Range<T>;
1009
1010 #[inline]
1011 fn intersect(self, rhs: RangeFrom<T>) -> Self::Intersection {
1012 Range { start: rhs.start, end: self.end }
1013 }
1014}
1015
1016#[unstable(feature = "range_intersect", issue = "165428")]
1017#[rustc_const_unstable(feature = "const_range", issue = "none")]
1018const impl<T> RangeIntersect for RangeTo<T>
1019where
1020 T: [const] Ord + [const] Destruct,
1021{
1022 type Intersection = RangeTo<T>;
1023
1024 #[inline]
1025 fn intersect(self, rhs: Self) -> Self::Intersection {
1026 RangeTo { end: Ord::min(self.end, rhs.end) }
1027 }
1028}
1029
1030#[unstable(feature = "range_intersect", issue = "165428")]
1031#[rustc_const_unstable(feature = "const_range", issue = "none")]
1032const impl<T> RangeIntersect<RangeFull> for RangeTo<T> {
1033 type Intersection = RangeTo<T>;
1034
1035 #[inline]
1036 fn intersect(self, _rhs: RangeFull) -> Self::Intersection {
1037 self
1038 }
1039}
1040
1041#[unstable(feature = "range_intersect", issue = "165428")]
1042#[rustc_const_unstable(feature = "const_range", issue = "none")]
1043const impl<T> RangeIntersect<Range<T>> for RangeFull {
1044 type Intersection = Range<T>;
1045
1046 #[inline]
1047 fn intersect(self, rhs: Range<T>) -> Self::Intersection {
1048 rhs
1049 }
1050}
1051
1052#[unstable(feature = "range_intersect", issue = "165428")]
1053#[rustc_const_unstable(feature = "const_range", issue = "none")]
1054const impl<T> RangeIntersect<RangeFrom<T>> for RangeFull {
1055 type Intersection = RangeFrom<T>;
1056
1057 #[inline]
1058 fn intersect(self, rhs: RangeFrom<T>) -> Self::Intersection {
1059 rhs
1060 }
1061}
1062
1063#[unstable(feature = "range_intersect", issue = "165428")]
1064#[rustc_const_unstable(feature = "const_range", issue = "none")]
1065const impl<T> RangeIntersect<RangeTo<T>> for RangeFull {
1066 type Intersection = RangeTo<T>;
1067
1068 #[inline]
1069 fn intersect(self, rhs: RangeTo<T>) -> Self::Intersection {
1070 rhs
1071 }
1072}
1073
1074#[unstable(feature = "range_intersect", issue = "165428")]
1075#[rustc_const_unstable(feature = "const_range", issue = "none")]
1076const impl RangeIntersect for RangeFull {
1077 type Intersection = RangeFull;
1078
1079 #[inline]
1080 fn intersect(self, _rhs: RangeFull) -> Self::Intersection {
1081 RangeFull
1082 }
1083}
1084
1085#[unstable(feature = "range_intersect", issue = "165428")]
1086#[rustc_const_unstable(feature = "const_range", issue = "none")]
1087const impl<T> RangeIntersect<RangeInclusive<T>> for RangeFull {
1088 type Intersection = RangeInclusive<T>;
1089
1090 #[inline]
1091 fn intersect(self, rhs: RangeInclusive<T>) -> Self::Intersection {
1092 rhs
1093 }
1094}
1095
1096#[unstable(feature = "range_intersect", issue = "165428")]
1097#[rustc_const_unstable(feature = "const_range", issue = "none")]
1098const impl<T> RangeIntersect<RangeToInclusive<T>> for RangeFull {
1099 type Intersection = RangeToInclusive<T>;
1100
1101 #[inline]
1102 fn intersect(self, rhs: RangeToInclusive<T>) -> Self::Intersection {
1103 rhs
1104 }
1105}
1106
1107#[unstable(feature = "range_intersect", issue = "165428")]
1108#[rustc_const_unstable(feature = "const_range", issue = "none")]
1109const impl<T> RangeIntersect<RangeFrom<T>> for RangeInclusive<T>
1110where
1111 T: [const] Ord + [const] Destruct,
1112{
1113 type Intersection = RangeInclusive<T>;
1114
1115 #[inline]
1116 fn intersect(self, rhs: RangeFrom<T>) -> Self::Intersection {
1117 RangeInclusive { start: Ord::max(self.start, rhs.start), last: self.last }
1118 }
1119}
1120
1121#[unstable(feature = "range_intersect", issue = "165428")]
1122#[rustc_const_unstable(feature = "const_range", issue = "none")]
1123const impl<T> RangeIntersect<RangeFull> for RangeInclusive<T> {
1124 type Intersection = RangeInclusive<T>;
1125
1126 #[inline]
1127 fn intersect(self, _rhs: RangeFull) -> Self::Intersection {
1128 self
1129 }
1130}
1131
1132#[unstable(feature = "range_intersect", issue = "165428")]
1133#[rustc_const_unstable(feature = "const_range", issue = "none")]
1134const impl<T> RangeIntersect for RangeInclusive<T>
1135where
1136 T: [const] Ord + [const] Destruct,
1137{
1138 type Intersection = RangeInclusive<T>;
1139
1140 #[inline]
1141 fn intersect(self, rhs: Self) -> Self::Intersection {
1142 RangeInclusive {
1143 start: Ord::max(self.start, rhs.start),
1144 last: Ord::min(self.last, rhs.last),
1145 }
1146 }
1147}
1148
1149#[unstable(feature = "range_intersect", issue = "165428")]
1150#[rustc_const_unstable(feature = "const_range", issue = "none")]
1151const impl<T> RangeIntersect<RangeToInclusive<T>> for RangeInclusive<T>
1152where
1153 T: [const] Ord + [const] Destruct,
1154{
1155 type Intersection = RangeInclusive<T>;
1156
1157 #[inline]
1158 fn intersect(self, rhs: RangeToInclusive<T>) -> Self::Intersection {
1159 RangeInclusive { start: self.start, last: Ord::min(self.last, rhs.last) }
1160 }
1161}
1162
1163#[unstable(feature = "range_intersect", issue = "165428")]
1164#[rustc_const_unstable(feature = "const_range", issue = "none")]
1165const impl<T> RangeIntersect<RangeFrom<T>> for RangeToInclusive<T> {
1166 type Intersection = RangeInclusive<T>;
1167
1168 #[inline]
1169 fn intersect(self, rhs: RangeFrom<T>) -> Self::Intersection {
1170 RangeInclusive { start: rhs.start, last: self.last }
1171 }
1172}
1173
1174#[unstable(feature = "range_intersect", issue = "165428")]
1175#[rustc_const_unstable(feature = "const_range", issue = "none")]
1176const impl<T> RangeIntersect<RangeFull> for RangeToInclusive<T> {
1177 type Intersection = RangeToInclusive<T>;
1178
1179 #[inline]
1180 fn intersect(self, _rhs: RangeFull) -> Self::Intersection {
1181 self
1182 }
1183}
1184
1185#[unstable(feature = "range_intersect", issue = "165428")]
1186#[rustc_const_unstable(feature = "const_range", issue = "none")]
1187const impl<T> RangeIntersect<RangeInclusive<T>> for RangeToInclusive<T>
1188where
1189 T: [const] Ord + [const] Destruct,
1190{
1191 type Intersection = RangeInclusive<T>;
1192
1193 #[inline]
1194 fn intersect(self, rhs: RangeInclusive<T>) -> Self::Intersection {
1195 RangeInclusive { start: rhs.start, last: Ord::min(self.last, rhs.last) }
1196 }
1197}
1198
1199#[unstable(feature = "range_intersect", issue = "165428")]
1200#[rustc_const_unstable(feature = "const_range", issue = "none")]
1201const impl<T> RangeIntersect for RangeToInclusive<T>
1202where
1203 T: [const] Ord + [const] Destruct,
1204{
1205 type Intersection = RangeToInclusive<T>;
1206
1207 #[inline]
1208 fn intersect(self, rhs: Self) -> Self::Intersection {
1209 RangeToInclusive { last: Ord::min(self.last, rhs.last) }
1210 }
1211}