Skip to content

PgLTree::from is not ergonomic #3948

@JerryQ17

Description

@JerryQ17

I have found these related issues/pull requests

none yet

Description

I impl From<MyType> for PgLTree, when I later tried to convert MyType to it by PgLTree::from(my_type), it resolved to the following from method.

pub struct PgLTree {
    labels: Vec<PgLTreeLabel>,
}

impl PgLTree {
    /// creates ltree from a [`Vec<PgLTreeLabel>`]
    pub fn from(labels: Vec<PgLTreeLabel>) -> Self {
        Self { labels }
    }
}

Because it's an associated function, it takes precedence over From trait method. I have to either:

  • use full qualified <PgLTree as From<MyType>>::from(my_type), or
  • use a temp var let ltree: PgLTree = my_type.into();

Both are annoying to write.

Prefered solution

Change it to:

impl From<Vec<PgLTreeLabel>> for PgLTree {
    fn from(labels: Vec<PgLTreeLabel>) -> Self {
        Self { labels }
    }
}

Is this a breaking change? Why or why not?

Yes, see playground.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions