Skip to content

Commit

Permalink
Use IntoBody and IntoBytes to write data (#10)
Browse files Browse the repository at this point in the history
* use generice methods to write data

* clean code

* update CHANGELOG
  • Loading branch information
atimin committed May 17, 2024
1 parent 698b51e commit 46f21da
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed

- Use IntoBody and `IntoBytes to write data, [PR-10](https://github.com/reductstore/reduct-rs/pull/10)

## [1.9.5] - 2024-03-30

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ async fn main() -> Result<(), ReductError> {
bucket
.write_record("entry-1")
.timestamp(timestamp)
.data(Bytes::from("Hello, World!"))
.data("Hello, World!")
.send()
.await?;

Expand Down
3 changes: 1 addition & 2 deletions examples/hallo_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

use bytes::Bytes;
use reduct_rs::{ReductClient, ReductError};
use std::str::from_utf8;
use std::time::SystemTime;
Expand All @@ -18,7 +17,7 @@ async fn main() -> Result<(), ReductError> {
bucket
.write_record("entry-1")
.timestamp(timestamp)
.data(Bytes::from("Hello, World!"))
.data("Hello, World!")
.send()
.await?;

Expand Down
5 changes: 2 additions & 3 deletions examples/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

use bytes::Bytes;
use futures_util::StreamExt;
use reduct_rs::{ReductClient, ReductError};
use std::str::from_utf8;
Expand All @@ -19,14 +18,14 @@ async fn main() -> Result<(), ReductError> {
bucket
.write_record("entry-1")
.add_label("planet", "Earth")
.data(Bytes::from("Hello, Earth!"))
.data("Hello, Earth!")
.send()
.await?;

bucket
.write_record("entry-1")
.add_label("planet", "Mars")
.data(Bytes::from("Hello, Mars!"))
.data("Hello, Mars!")
.send()
.await?;

Expand Down
10 changes: 8 additions & 2 deletions src/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,15 +157,21 @@ impl RecordBuilder {
}

/// Set the content length of the record to write
///
/// Note: use this with stream data
pub fn content_length(mut self, content_length: usize) -> Self {
self.record.content_length = content_length;
self
}

/// Set the content of the record
///
/// Overwrites content length
pub fn data(mut self, bytes: Bytes) -> Self {
/// Note: use this with data that fits in memory
pub fn data<D>(mut self, data: D) -> Self
where
D: Into<Bytes>,
{
let bytes = data.into();
self.record.content_length = bytes.len();
self.record.data = Some(Box::pin(futures::stream::once(async move { Ok(bytes) })));
self
Expand Down
9 changes: 8 additions & 1 deletion src/record/write_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,14 @@ impl WriteRecordBuilder {
}

/// Set the data of the record to write.
pub fn data(mut self, data: Bytes) -> Self {
///
/// # Arguments
///
/// * `data` - The data to write.
pub fn data<B>(mut self, data: B) -> Self
where
B: Into<Body>,
{
self.data = Some(data.into());
self
}
Expand Down

0 comments on commit 46f21da

Please sign in to comment.