Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(rust): Add FromIterator impls for PlSmallStr #18509

Merged
merged 2 commits into from
Sep 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 85 additions & 3 deletions crates/polars-utils/src/pl_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ macro_rules! format_pl_smallstr {
($($arg:tt)*) => {{
use std::fmt::Write;

let mut string = String::new();
let mut string = PlSmallStr::EMPTY;
write!(string, $($arg)*).unwrap();
PlSmallStr::from_string(string)
string
}}
}

Expand Down Expand Up @@ -125,7 +125,70 @@ impl From<&String> for PlSmallStr {
}
}

/// FromIterator impls (TODO)
impl From<Inner> for PlSmallStr {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

drive-by - add From<Inner>

#[inline(always)]
fn from(value: Inner) -> Self {
Self(value)
}
}

/// FromIterator impls

impl FromIterator<PlSmallStr> for PlSmallStr {
#[inline(always)]
fn from_iter<T: IntoIterator<Item = PlSmallStr>>(iter: T) -> Self {
Self(Inner::from_iter(iter.into_iter().map(|x| x.0)))
}
}

impl<'a> FromIterator<&'a PlSmallStr> for PlSmallStr {
#[inline(always)]
fn from_iter<T: IntoIterator<Item = &'a PlSmallStr>>(iter: T) -> Self {
Self(Inner::from_iter(iter.into_iter().map(|x| x.as_str())))
}
}

impl FromIterator<char> for PlSmallStr {
#[inline(always)]
fn from_iter<I: IntoIterator<Item = char>>(iter: I) -> PlSmallStr {
Self(Inner::from_iter(iter))
}
}

impl<'a> FromIterator<&'a char> for PlSmallStr {
#[inline(always)]
fn from_iter<I: IntoIterator<Item = &'a char>>(iter: I) -> PlSmallStr {
Self(Inner::from_iter(iter))
}
}

impl<'a> FromIterator<&'a str> for PlSmallStr {
#[inline(always)]
fn from_iter<I: IntoIterator<Item = &'a str>>(iter: I) -> PlSmallStr {
Self(Inner::from_iter(iter))
}
}

impl FromIterator<String> for PlSmallStr {
#[inline(always)]
fn from_iter<I: IntoIterator<Item = String>>(iter: I) -> PlSmallStr {
Self(Inner::from_iter(iter))
}
}

impl FromIterator<Box<str>> for PlSmallStr {
#[inline(always)]
fn from_iter<I: IntoIterator<Item = Box<str>>>(iter: I) -> PlSmallStr {
Self(Inner::from_iter(iter))
}
}

impl<'a> FromIterator<std::borrow::Cow<'a, str>> for PlSmallStr {
#[inline(always)]
fn from_iter<I: IntoIterator<Item = std::borrow::Cow<'a, str>>>(iter: I) -> PlSmallStr {
Self(Inner::from_iter(iter))
}
}

/// PartialEq impls

Expand Down Expand Up @@ -153,6 +216,25 @@ impl PartialEq<PlSmallStr> for String {
}
}

/// Write

impl core::fmt::Write for PlSmallStr {
#[inline(always)]
fn write_char(&mut self, c: char) -> std::fmt::Result {
self.0.write_char(c)
}

#[inline(always)]
fn write_fmt(&mut self, args: std::fmt::Arguments<'_>) -> std::fmt::Result {
self.0.write_fmt(args)
}

#[inline(always)]
fn write_str(&mut self, s: &str) -> std::fmt::Result {
self.0.write_str(s)
}
}

/// Debug, Display

impl core::fmt::Debug for PlSmallStr {
Expand Down