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

Update ToWkt::write_wkt to not buffer string #126

Merged
merged 6 commits into from
Nov 27, 2024
Merged
Changes from 4 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
17 changes: 15 additions & 2 deletions src/to_wkt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ pub use geo_trait_impl::{
write_rect, write_triangle,
};

/// A wrapper around something that implements std::io::Write to be used with our writer traits,
/// which require std::fmt::Write
struct WriterWrapper<W: std::io::Write>(W);

impl<W: std::io::Write> std::fmt::Write for WriterWrapper<W> {
fn write_str(&mut self, s: &str) -> std::fmt::Result {
// Sadly, this will lose the content of the error when mapping to std::fmt::Error
self.0.write(s.as_bytes()).map_err(|_| std::fmt::Error)?;
Copy link
Member Author

Choose a reason for hiding this comment

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

This loss of error content is unfortunate but I'm not sure of a good way around this.

Copy link
Member

Choose a reason for hiding this comment

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

See #129

Ok(())
}
}

/// A trait for converting values to WKT
pub trait ToWkt<T>
where
Expand Down Expand Up @@ -49,7 +61,8 @@ where
///
/// assert_eq!(wkt_string, "POINT(1.2 3.4)");
/// ```
fn write_wkt(&self, mut writer: impl std::io::Write) -> std::io::Result<()> {
writer.write_all(self.wkt_string().as_bytes())
fn write_wkt(&self, writer: impl std::io::Write) -> std::io::Result<()> {
write_geometry(&mut WriterWrapper(writer), &self.to_wkt())
.map_err(|err| std::io::Error::new(std::io::ErrorKind::Other, err.to_string()))
}
}
Loading