Skip to content

flatten and flatten_ref methods on ActiveValue - #3146

Open
PacificBird wants to merge 3 commits into
SeaQL:masterfrom
PacificBird:flatten_as_ref
Open

flatten and flatten_ref methods on ActiveValue#3146
PacificBird wants to merge 3 commits into
SeaQL:masterfrom
PacificBird:flatten_as_ref

Conversation

@PacificBird

Copy link
Copy Markdown
Contributor

Closes #3145

@Huliiiiii

Copy link
Copy Markdown
Member

Why not just:

impl<V> ActiveValue<Option<V>>
where
    V: Into<Value> + Nullable, 
{
...
}

@Huliiiiii

Copy link
Copy Markdown
Member

Naming them as_option and into_option might be better.

@PacificBird

Copy link
Copy Markdown
Contributor Author

Why not just:

impl<V> ActiveValue<Option<V>>
where
    V: Into<Value> + Nullable, 
{
...
}

This requires specialization afaik. The impl<V> ActiveValue<V> gives a conflicting implementation as far as the compiler is currently concerned.

@PacificBird

Copy link
Copy Markdown
Contributor Author

Naming them as_option and into_option might be better.

That's definitely shorter and nicer.

@Huliiiiii

Huliiiiii commented Jul 24, 2026

Copy link
Copy Markdown
Member

Why not just:

impl<V> ActiveValue<Option<V>>
where
    V: Into<Value> + Nullable, 
{
...
}

This requires specialization afaik. The impl<V> ActiveValue<V> gives a conflicting implementation as far as the compiler is currently concerned.

Implementing it on Option<V> instead of V works correctly. (on my machine)

impl<V> ActiveValue<Option<V>>
where
    V: Into<Value> + Nullable,
{
    pub fn as_option(&self) -> Option<&V> {
        self.try_as_ref().and_then(Option::as_ref)
    }
 
    pub fn into_option(self) -> Option<V> {
        match self {
            Self::Set(value) | Self::Unchanged(value) => value,
            Self::NotSet => None,
        }
    }
}

@tyt2y3

tyt2y3 commented Jul 31, 2026

Copy link
Copy Markdown
Member

Agree with @Huliiiiii's approach — implementing on ActiveValue<Option<V>> drops the two sealed helper traits entirely and, as a bonus, sidesteps the compile errors the current trait version has:

  • the AsRefOption<T> impl is missing its lifetime arg (AsRefOption<'a, T>),
  • the flatten_ref bound constrains the owned V instead of &V, so it's unsatisfiable for any real caller,
  • flatten(&self) can't move out of &self to return an owned value.

CI is red on all three doctests.

The as_option / into_option naming is good. One thing the rename gets for free: into_option takes self by value (matching the into_ convention), which is exactly the fix flatten needed.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Flatten methods on ActiveValue

3 participants