Skip to content

RangeInclusive has pub(crate) fields. This could be pub. #67371

Description

@Byter09

Hello.

I'm currently writing a library that generalizes all ranges and adds a "range set".
Core/Std compatibility is a must and I want to make the crate "feel" as if it would already be part of them. This, of course, requires From implementations for all range types for my GenericRange<T>.

All cases, for example Range<T> are trivial:

impl<T> From<Range<T>> for GenericRange<T>
where
    T: PartialOrd,
{
    fn from(range: Range<T>) -> Self {
        Self::closed_open(range.start, range.end)
    }
}

(note: this requires PartialOrd, because upon converting I assert, that start <= end to optimize other code that depends on this requirement)

The problem I'm facing now is that I want to keep clones at a minimum, but RangeInclusive<T> forces me to commit this atrocity:

impl<T> From<RangeInclusive<T>> for GenericRange<T>
where
    T: Clone + PartialOrd,
{
    /// This requires `Clone` because for some reason the internal fields of `RangeInclusive` are
    /// only `pub(crate)` even though they could be `pub` and so we're forced to use the results of
    /// `RangeBounds`, which only return borrowed values.
    fn from(range: RangeInclusive<T>) -> Self {
        Self::closed(range.start().clone(), range.end().clone())
    }
}

As mentioned in the doc comment and in the title of this issue, RangeInclusive<T> has pub(crate) fields, which prohibits other users, like me, to correctly take the fields without copying.

The crate is forbid(unsafe_code) and #![no_std] and I have thus no other way to implement From other than cloning the RangeBounds trait results.

If there is a reason for this I'm sorry for opening an issue - but I don't see one.

Thanks for taking the time to look at this!

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-feature-requestCategory: A feature request, i.e: not implemented / a PR.T-libs-api[DEPRECATED; DO NOT USE]

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions