Skip to content

Allow (de)serialization of Cow<'_, T> values#1545

Merged
Lorak-mmk merged 1 commit intoscylladb:mainfrom
nrxus:serialize-cow-str
Jan 20, 2026
Merged

Allow (de)serialization of Cow<'_, T> values#1545
Lorak-mmk merged 1 commit intoscylladb:mainfrom
nrxus:serialize-cow-str

Conversation

@nrxus
Copy link
Contributor

@nrxus nrxus commented Jan 14, 2026

There is already precedence for doing this for Arc and Box so this PR merely extends the same functionality to a another stdlib smart pointer.

In particular this is useful for me when doing a maybe borrowed deserialization in serde where the string may or may not need to be owned (depending on whether the string was escaped in the input). This maybe borrowed deserialization is done by storing it in a Cow<'_, str>. I want to then store that Cow<'_, str> as TEXT in Scylla without too much work but prior to this PR that was not doable.

While I personally only care about serializing Cow<'_, str> I figured the driver should be more general and allow for any T and for both serialization and deserialization as it is done for Arc and Box. The main difference is that I didn't need to special case Cow as a generic implementation works just fine.

Pre-review checklist

  • I have split my patch into logically separate commits.
  • All commit messages clearly explain what they change and why.
  • I added relevant tests for new features and bug fixes.
  • All commits compile, pass static checks and pass test.
  • PR description sums up the changes and reasons why they should be introduced.
  • I have provided docstrings for the public items that I want to introduce.
  • I have adjusted the documentation in ./docs/source/.
  • I added appropriate Fixes: annotations to PR description.

@github-actions
Copy link

github-actions bot commented Jan 14, 2026

cargo semver-checks found no API-breaking changes in this PR.
Checked commit: 820f304

Copy link
Collaborator

@Lorak-mmk Lorak-mmk left a comment

Choose a reason for hiding this comment

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

Thanks! I left just a few minor comments.

Comment on lines +1621 to +1638
impl<'frame, 'metadata, T: 'frame + ToOwned + ?Sized> DeserializeValue<'frame, 'metadata>
for Cow<'frame, T>
where
&'frame T: DeserializeValue<'frame, 'metadata>,
{
fn type_check(typ: &ColumnType) -> Result<(), TypeCheckError> {
<&T>::type_check(typ).map_err(typck_error_replace_rust_name::<Self>)
}

fn deserialize(
typ: &'metadata ColumnType<'metadata>,
v: Option<FrameSlice<'frame>>,
) -> Result<Self, DeserializationError> {
<&T>::deserialize(typ, v)
.map(Cow::Borrowed)
.map_err(deser_error_replace_rust_name::<Self>)
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

It's unfortunate that Rust doesn't have lifetime specialization (yes, I know there are big soundness issues with it). It would be great to use the Owned variant when deserializing to Cow<'static, T>, but that's not possible (at least for now).

Copy link
Collaborator

Choose a reason for hiding this comment

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

In docs/source/data-types/data-types.md there is the following line:
Additionally, BoxandArc serialization and deserialization is supported for all above types.

Could you modify it to also mention Cow?

@Lorak-mmk Lorak-mmk added this to the 1.5.0 milestone Jan 15, 2026
There is already precedence for doing this for Arc<T> and Box<T>
@nrxus nrxus force-pushed the serialize-cow-str branch from 02efebe to 820f304 Compare January 20, 2026 20:37
@nrxus nrxus requested a review from Lorak-mmk January 20, 2026 20:38
Copy link
Collaborator

@Lorak-mmk Lorak-mmk left a comment

Choose a reason for hiding this comment

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

Thanks!

@Lorak-mmk Lorak-mmk merged commit 9988e3b into scylladb:main Jan 20, 2026
12 checks passed
@wprzytula wprzytula mentioned this pull request Jan 29, 2026
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.

2 participants