geo icon indicating copy to clipboard operation
geo copied to clipboard

Question - associated CoordTypes in geo_traits without lifetime specifiers

Open AndrewLipscomb opened this issue 9 months ago • 3 comments

I have a little struct that looks like this

struct LineWrapper<T>
where
    T: geo_traits::LineTrait,
{
    pub point: <T as geo_traits::LineTrait>::CoordType<'_>,
    pub line: T,
}

impl<T> LineWrapper<T>
where
    T: geo_traits::LineTrait,
    <T as geo_traits::LineTrait>::CoordType<'_>: Clone,
{
    fn new(line: T) -> Self {
        let start = line.start().clone();
        Self { point: start, line }
    }
}

Rather fittingly - its telling me that the lifetime specifier needs to be set - fair enough, but I don't want to store that with respect to the lines lifetime, I want to clone it.

With the library as-is - can I strip out the lifetime from CoordType somehow to just get the type, and then I apply more constraints from there?

For comparison - this is all happy if I was to make my own trait with a seperate associated type for just the coord with no lifetime mention. So is there a way to get CoordTypeNoLife from CoordType<'a> ?

pub trait LineishTrait: Sized {
    /// The coordinate type of this geometry
    type T;

    type CoordTypeNoLife: geo_traits::CoordTrait<T = Self::T>;
    /// The type of each underlying coordinate, which implements [CoordTrait]
    type CoordType<'a>: 'a + geo_traits::CoordTrait<T = Self::T>
    where
        Self: 'a;

    /// The dimension of this geometry
    fn dim(&self) -> geo_traits::Dimensions;

    /// Access the start coordinate in this Line
    fn start(&self) -> Self::CoordType<'_>;

    /// Access the start coordinate in this Line
    fn end(&self) -> Self::CoordType<'_>;

    /// Access the two underlying coordinates
    fn coords(&self) -> [Self::CoordType<'_>; 2] {
        [self.start(), self.end()]
    }
}

struct LineWrapper<T>
where
    T: LineishTrait,
{
    pub point: <T as LineishTrait>::CoordTypeNoLife,
    pub line: T,
}

impl<T> LineWrapper<T>
where
    T: LineishTrait,
    <T as LineishTrait>::CoordTypeNoLife: Clone,
{
    fn new(line: T) -> Self {
        let start = line.start().clone();
        Self { point: start, line }
    }
}

I appreciate this is more a general Rust question - but if thats not possible and this is worth a PR - I'm happy to do that.

AndrewLipscomb avatar Mar 28 '25 03:03 AndrewLipscomb

I can appreciate that would be a silly breaking change for the geo_traits API as you'd have to define another associated type

impl LineishTrait for NodeLine {
    type T = f64;
    type CoordTypeNoLife = Node;
    type CoordType<'a> = Node;

    ... snip ... 
}

I think for now I'm going to make my own trait for exposing that one out within my crate - I can pull in geo types' adherence for myself there.

For reference - the thing I am trying to look for in terms of equivalent functionality is something like https://beta.boost.org/doc/libs/1_72_0/libs/geometry/doc/html/geometry/reference/core/point_type.html - point_type uses some SFINAE silliness to get out the appropriate point type for any geometry type.

AFAICS - that is basically what the CoordType type does here - except with lifetimes.

If I am barking up the wrong tree here and this exists somewhere else in geo - let me know :)

AndrewLipscomb avatar Mar 28 '25 04:03 AndrewLipscomb

Doing more reading on lifetimes places within what was added with 2018 Rust and generic associated types. I get the feeling the answer here is no - because this would make it impossible to specify for a type which cannot exist without a named lifetime. The lifetime is intrinsic to the type's definition if it needs to be explicitly set.

If I have any of this wrong - let me know, I'd love to know a "yes, you can" way to do this - but otherwise I will sort out my own traits to model what I want here.

AndrewLipscomb avatar Mar 28 '25 04:03 AndrewLipscomb

Does this change help you? https://github.com/georust/geo/pull/1348

frewsxcv avatar Apr 24 '25 00:04 frewsxcv

I think https://github.com/georust/geo/pull/1348 might have resolved this for you, but if it didn't please reopen

frewsxcv avatar Nov 15 '25 17:11 frewsxcv