diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-04 12:41:41 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-04 12:41:41 +0000 |
commit | 10ee2acdd26a7f1298c6f6d6b7af9b469fe29b87 (patch) | |
tree | bdffd5d80c26cf4a7a518281a204be1ace85b4c1 /vendor/gix-config/src/parse/comment.rs | |
parent | Releasing progress-linux version 1.70.0+dfsg1-9~progress7.99u1. (diff) | |
download | rustc-10ee2acdd26a7f1298c6f6d6b7af9b469fe29b87.tar.xz rustc-10ee2acdd26a7f1298c6f6d6b7af9b469fe29b87.zip |
Merging upstream version 1.70.0+dfsg2.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/gix-config/src/parse/comment.rs')
-rw-r--r-- | vendor/gix-config/src/parse/comment.rs | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/vendor/gix-config/src/parse/comment.rs b/vendor/gix-config/src/parse/comment.rs new file mode 100644 index 000000000..6d4bb15ff --- /dev/null +++ b/vendor/gix-config/src/parse/comment.rs @@ -0,0 +1,50 @@ +use std::{borrow::Cow, fmt::Display}; + +use bstr::BString; + +use crate::parse::Comment; + +impl Comment<'_> { + /// Turn this instance into a fully owned one with `'static` lifetime. + #[must_use] + pub fn to_owned(&self) -> Comment<'static> { + Comment { + tag: self.tag, + text: Cow::Owned(self.text.as_ref().into()), + } + } + + /// Serialize this type into a `BString` for convenience. + /// + /// Note that `to_string()` can also be used, but might not be lossless. + #[must_use] + pub fn to_bstring(&self) -> BString { + let mut buf = Vec::new(); + self.write_to(&mut buf).expect("io error impossible"); + buf.into() + } + + /// Stream ourselves to the given `out`, in order to reproduce this comment losslessly. + pub fn write_to(&self, mut out: impl std::io::Write) -> std::io::Result<()> { + out.write_all(&[self.tag])?; + out.write_all(self.text.as_ref()) + } +} + +impl Display for Comment<'_> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + Display::fmt(&self.to_bstring(), f) + } +} + +impl From<Comment<'_>> for BString { + fn from(c: Comment<'_>) -> Self { + c.into() + } +} + +impl From<&Comment<'_>> for BString { + fn from(c: &Comment<'_>) -> Self { + c.to_bstring() + } +} |