This is an automated email from the ASF dual-hosted git repository.

silver pushed a commit to branch cpp-more-operation
in repository https://gitbox.apache.org/repos/asf/incubator-opendal.git


The following commit(s) were added to refs/heads/cpp-more-operation by this 
push:
     new 145ed0822 refactor rust types into different files
145ed0822 is described below

commit 145ed082269103c99a7bc73f42f479078db0312a
Author: silver-ymz <[email protected]>
AuthorDate: Tue Sep 5 16:42:08 2023 +0800

    refactor rust types into different files
    
    Signed-off-by: silver-ymz <[email protected]>
---
 bindings/cpp/include/opendal.hpp |  8 ++--
 bindings/cpp/src/lib.rs          | 87 +++-------------------------------------
 bindings/cpp/src/opendal.cpp     |  4 +-
 bindings/cpp/src/reader.rs       | 40 ++++++++++++++++++
 bindings/cpp/src/types.rs        | 78 +++++++++++++++++++++++++++++++++++
 5 files changed, 129 insertions(+), 88 deletions(-)

diff --git a/bindings/cpp/include/opendal.hpp b/bindings/cpp/include/opendal.hpp
index 20f8d08ab..80facdc5b 100644
--- a/bindings/cpp/include/opendal.hpp
+++ b/bindings/cpp/include/opendal.hpp
@@ -201,16 +201,14 @@ private:
 class Reader
     : public boost::iostreams::device<boost::iostreams::input_seekable> {
 public:
-  // Users should not use this type directly.
-  using InternalReader = rust::Box<opendal::ffi::Reader>;
-
-  Reader(InternalReader &&reader) : reader_(std::move(reader)) {}
+  Reader(rust::Box<opendal::ffi::Reader> &&reader)
+      : raw_reader_(std::move(reader)) {}
 
   std::streamsize read(void *s, std::streamsize n);
   std::streampos seek(std::streamoff off, std::ios_base::seekdir way);
 
 private:
-  InternalReader reader_;
+  rust::Box<opendal::ffi::Reader> raw_reader_;
 };
 
 // Boost IOStreams requires it to be copyable. So we need to use
diff --git a/bindings/cpp/src/lib.rs b/bindings/cpp/src/lib.rs
index c1a94fd4b..8f4bc850b 100644
--- a/bindings/cpp/src/lib.rs
+++ b/bindings/cpp/src/lib.rs
@@ -15,9 +15,12 @@
 // specific language governing permissions and limitations
 // under the License.
 
+mod reader;
+mod types;
+
 use anyhow::Result;
-use od::raw::oio::BlockingRead;
 use opendal as od;
+use reader::Reader;
 use std::str::FromStr;
 
 #[cxx::bridge(namespace = "opendal::ffi")]
@@ -76,14 +79,12 @@ mod ffi {
         fn list(self: &Operator, path: &str) -> Result<Vec<Entry>>;
         fn reader(self: &Operator, path: &str) -> Result<Box<Reader>>;
 
-        #[cxx_name = "read"]
-        fn reader_read(self: &mut Reader, buf: &mut [u8]) -> Result<usize>;
+        fn read(self: &mut Reader, buf: &mut [u8]) -> Result<usize>;
         fn seek(self: &mut Reader, offset: u64, dir: SeekFrom) -> Result<u64>;
     }
 }
 
-struct Operator(od::BlockingOperator);
-struct Reader(od::BlockingReader);
+pub struct Operator(od::BlockingOperator);
 
 fn new_operator(scheme: &str, configs: Vec<ffi::HashMapValue>) -> 
Result<Box<Operator>> {
     let scheme = od::Scheme::from_str(scheme)?;
@@ -144,79 +145,3 @@ impl Operator {
         Ok(Box::new(Reader(self.0.reader(path)?)))
     }
 }
-
-impl Reader {
-    fn reader_read(&mut self, buf: &mut [u8]) -> Result<usize> {
-        Ok(self.0.read(buf)?)
-    }
-
-    fn seek(&mut self, offset: u64, dir: ffi::SeekFrom) -> Result<u64> {
-        let pos = match dir {
-            ffi::SeekFrom::Start => std::io::SeekFrom::Start(offset),
-            ffi::SeekFrom::Current => std::io::SeekFrom::Current(offset as 
i64),
-            ffi::SeekFrom::End => std::io::SeekFrom::End(offset as i64),
-            _ => return Err(anyhow::anyhow!("invalid seek dir")),
-        };
-
-        Ok(self.0.seek(pos)?)
-    }
-}
-
-impl From<od::Metadata> for ffi::Metadata {
-    fn from(meta: od::Metadata) -> Self {
-        let mode = meta.mode().into();
-        let content_length = meta.content_length();
-        let cache_control = meta.cache_control().map(ToOwned::to_owned).into();
-        let content_disposition = 
meta.content_disposition().map(ToOwned::to_owned).into();
-        let content_md5 = meta.content_md5().map(ToOwned::to_owned).into();
-        let content_type = meta.content_type().map(ToOwned::to_owned).into();
-        let etag = meta.etag().map(ToOwned::to_owned).into();
-        let last_modified = meta
-            .last_modified()
-            .map(|time| time.to_rfc3339_opts(chrono::SecondsFormat::Nanos, 
false))
-            .into();
-
-        Self {
-            mode,
-            content_length,
-            cache_control,
-            content_disposition,
-            content_md5,
-            content_type,
-            etag,
-            last_modified,
-        }
-    }
-}
-
-impl From<od::Entry> for ffi::Entry {
-    fn from(entry: od::Entry) -> Self {
-        let (path, _) = entry.into_parts();
-        Self { path }
-    }
-}
-
-impl From<od::EntryMode> for ffi::EntryMode {
-    fn from(mode: od::EntryMode) -> Self {
-        match mode {
-            od::EntryMode::FILE => Self::File,
-            od::EntryMode::DIR => Self::Dir,
-            _ => Self::Unknown,
-        }
-    }
-}
-
-impl From<Option<String>> for ffi::OptionalString {
-    fn from(s: Option<String>) -> Self {
-        match s {
-            Some(s) => Self {
-                has_value: true,
-                value: s,
-            },
-            None => Self {
-                has_value: false,
-                value: String::default(),
-            },
-        }
-    }
-}
diff --git a/bindings/cpp/src/opendal.cpp b/bindings/cpp/src/opendal.cpp
index 7c23ebb41..e8ffa75a8 100644
--- a/bindings/cpp/src/opendal.cpp
+++ b/bindings/cpp/src/opendal.cpp
@@ -95,14 +95,14 @@ Reader Operator::reader(std::string_view path) {
 
 std::streamsize Reader::read(void *s, std::streamsize n) {
   auto rust_slice = rust::Slice<uint8_t>(reinterpret_cast<uint8_t *>(s), n);
-  auto read_size = reader_->read(rust_slice);
+  auto read_size = raw_reader_->read(rust_slice);
   return read_size;
 }
 
 ffi::SeekDir to_rust_seek_dir(std::ios_base::seekdir dir);
 
 std::streampos Reader::seek(std::streamoff off, std::ios_base::seekdir dir) {
-  return reader_->seek(off, to_rust_seek_dir(dir));
+  return raw_reader_->seek(off, to_rust_seek_dir(dir));
 }
 
 // Metadata
diff --git a/bindings/cpp/src/reader.rs b/bindings/cpp/src/reader.rs
new file mode 100644
index 000000000..9ad55d714
--- /dev/null
+++ b/bindings/cpp/src/reader.rs
@@ -0,0 +1,40 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use super::ffi;
+use anyhow::Result;
+use od::raw::oio::BlockingRead;
+use opendal as od;
+
+pub struct Reader(pub od::BlockingReader);
+
+impl Reader {
+    pub fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
+        Ok(self.0.read(buf)?)
+    }
+
+    pub fn seek(&mut self, offset: u64, dir: ffi::SeekFrom) -> Result<u64> {
+        let pos = match dir {
+            ffi::SeekFrom::Start => std::io::SeekFrom::Start(offset),
+            ffi::SeekFrom::Current => std::io::SeekFrom::Current(offset as 
i64),
+            ffi::SeekFrom::End => std::io::SeekFrom::End(offset as i64),
+            _ => return Err(anyhow::anyhow!("invalid seek dir")),
+        };
+
+        Ok(self.0.seek(pos)?)
+    }
+}
diff --git a/bindings/cpp/src/types.rs b/bindings/cpp/src/types.rs
new file mode 100644
index 000000000..ccd456716
--- /dev/null
+++ b/bindings/cpp/src/types.rs
@@ -0,0 +1,78 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use super::ffi;
+use opendal as od;
+
+impl From<od::Metadata> for ffi::Metadata {
+    fn from(meta: od::Metadata) -> Self {
+        let mode = meta.mode().into();
+        let content_length = meta.content_length();
+        let cache_control = meta.cache_control().map(ToOwned::to_owned).into();
+        let content_disposition = 
meta.content_disposition().map(ToOwned::to_owned).into();
+        let content_md5 = meta.content_md5().map(ToOwned::to_owned).into();
+        let content_type = meta.content_type().map(ToOwned::to_owned).into();
+        let etag = meta.etag().map(ToOwned::to_owned).into();
+        let last_modified = meta
+            .last_modified()
+            .map(|time| time.to_rfc3339_opts(chrono::SecondsFormat::Nanos, 
false))
+            .into();
+
+        Self {
+            mode,
+            content_length,
+            cache_control,
+            content_disposition,
+            content_md5,
+            content_type,
+            etag,
+            last_modified,
+        }
+    }
+}
+
+impl From<od::Entry> for ffi::Entry {
+    fn from(entry: od::Entry) -> Self {
+        let (path, _) = entry.into_parts();
+        Self { path }
+    }
+}
+
+impl From<od::EntryMode> for ffi::EntryMode {
+    fn from(mode: od::EntryMode) -> Self {
+        match mode {
+            od::EntryMode::FILE => Self::File,
+            od::EntryMode::DIR => Self::Dir,
+            _ => Self::Unknown,
+        }
+    }
+}
+
+impl From<Option<String>> for ffi::OptionalString {
+    fn from(s: Option<String>) -> Self {
+        match s {
+            Some(s) => Self {
+                has_value: true,
+                value: s,
+            },
+            None => Self {
+                has_value: false,
+                value: String::default(),
+            },
+        }
+    }
+}

Reply via email to