Skip to content

how do I write custom types to a CSV writer? #319

Answered by BurntSushi
stevenliebregt asked this question in Q&A
Discussion options

You must be logged in to vote

This is probably a good example of the XY Problem. That is, it looks like the actual problem you're trying to solve is, "how do I conveniently convert my custom data types to CSV data." But here, you're asking about how to implement AsRef in a way that doesn't result in returning a reference. The answer to the latter question is that you can't. write_record is only for types that are known to be cheaply convertible to &[u8] via the AsRef trait. If you don't have that, then you can't use write_record.

In your case, TagValue can almost implement AsRef<[u8]>. If you really want to go that route, then I'd suggest converting TagValue into a new type that does the necessary conversions, and the…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by BurntSushi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #318 on May 11, 2023 15:54.