duesee / imap-codec

Rock-solid and complete codec for IMAP
Apache License 2.0
39 stars 14 forks source link

question: Conversions for `NonEmptyVec<T>`. #294

Closed duesee closed 1 year ago

duesee commented 1 year ago

What is the best way to construct a NonEmptyVec?

Currently, we have ...

impl<T> From<T> for NonEmptyVec<T> {
    fn from(value: T) -> Self {
        NonEmptyVec(vec![value])
    }
}

impl<T> TryFrom<Vec<T>> for NonEmptyVec<T> {
    type Error = NonEmptyVecError;

    fn try_from(inner: Vec<T>) -> Result<Self, Self::Error> {
        Self::validate(&inner)?;

        Ok(Self(inner))
    }
}

However, this could be confusing. Try for yourself: What is the difference between NonEmptyVec::from(vec![]) and NonEmptyVec::try_from(vec![])? :-)

References:

duesee commented 1 year ago

I think this is fine for now.