Xuanwo commented on code in PR #3001:
URL: 
https://github.com/apache/incubator-opendal/pull/3001#discussion_r1313243471


##########
bindings/cpp/src/lib.rs:
##########
@@ -56,7 +83,120 @@ impl Operator {
         Ok(self.0.read(path)?)
     }
 
-    fn write(&self, path: &str, bs: &[u8]) -> Result<()> {
-        Ok(self.0.write(path, bs.to_owned())?)
+    // To avoid copying the bytes, we use &'static [u8] here.
+    //
+    // Safety: The bytes created from bs will be dropped after the function 
call.
+    // So it's safe to declare its lifetime as 'static.
+    fn write(&self, path: &str, bs: &'static [u8]) -> Result<()> {
+        Ok(self.0.write(path, bs)?)
+    }
+
+    fn is_exist(&self, path: &str) -> Result<bool> {
+        Ok(self.0.is_exist(path)?)
+    }
+
+    fn create_dir(&self, path: &str) -> Result<()> {
+        Ok(self.0.create_dir(path)?)
+    }
+
+    fn copy(&self, src: &str, dst: &str) -> Result<()> {
+        Ok(self.0.copy(src, dst)?)
+    }
+
+    fn rename(&self, src: &str, dst: &str) -> Result<()> {
+        Ok(self.0.rename(src, dst)?)
+    }
+
+    // We can't name it to delete because it's a keyword in C++
+    fn remove(&self, path: &str) -> Result<()> {
+        Ok(self.0.delete(path)?)
+    }
+
+    fn stat(&self, path: &str) -> Result<ffi::Metadata> {
+        Ok(self.0.stat(path)?.into())
+    }
+
+    fn list(&self, path: &str) -> Result<Vec<ffi::Entry>> {
+        Ok(self.0.list(path)?.into_iter().map(Into::into).collect())
+    }
+}
+
+impl From<od::Metadata> for ffi::Metadata {
+    fn from(meta: od::Metadata) -> Self {
+        let mut tag = 0u8;
+
+        match meta.mode() {
+            od::EntryMode::FILE => tag |= 0b0000_0001,
+            od::EntryMode::DIR => tag |= 0b0000_0010,
+            _ => {}
+        }
+
+        let content_length = meta.content_length();
+
+        let cache_control = match meta.cache_control() {
+            Some(v) => {
+                tag |= 0b0000_0100;
+                v.to_owned()
+            }
+            None => String::new(),

Review Comment:
   > We can make `ffi::Metadata` store `&str` rather than `String` to avoid 
unnecessary construct about `String`.
   
   We don't need to polish here. We can copy and paste it directly here. We can 
optimize this part until we are certain that it won't affect our performance.



##########
bindings/cpp/src/lib.rs:
##########
@@ -56,7 +83,120 @@ impl Operator {
         Ok(self.0.read(path)?)
     }
 
-    fn write(&self, path: &str, bs: &[u8]) -> Result<()> {
-        Ok(self.0.write(path, bs.to_owned())?)
+    // To avoid copying the bytes, we use &'static [u8] here.
+    //
+    // Safety: The bytes created from bs will be dropped after the function 
call.
+    // So it's safe to declare its lifetime as 'static.
+    fn write(&self, path: &str, bs: &'static [u8]) -> Result<()> {
+        Ok(self.0.write(path, bs)?)
+    }
+
+    fn is_exist(&self, path: &str) -> Result<bool> {
+        Ok(self.0.is_exist(path)?)
+    }
+
+    fn create_dir(&self, path: &str) -> Result<()> {
+        Ok(self.0.create_dir(path)?)
+    }
+
+    fn copy(&self, src: &str, dst: &str) -> Result<()> {
+        Ok(self.0.copy(src, dst)?)
+    }
+
+    fn rename(&self, src: &str, dst: &str) -> Result<()> {
+        Ok(self.0.rename(src, dst)?)
+    }
+
+    // We can't name it to delete because it's a keyword in C++
+    fn remove(&self, path: &str) -> Result<()> {
+        Ok(self.0.delete(path)?)
+    }
+
+    fn stat(&self, path: &str) -> Result<ffi::Metadata> {
+        Ok(self.0.stat(path)?.into())
+    }
+
+    fn list(&self, path: &str) -> Result<Vec<ffi::Entry>> {
+        Ok(self.0.list(path)?.into_iter().map(Into::into).collect())
+    }
+}
+
+impl From<od::Metadata> for ffi::Metadata {
+    fn from(meta: od::Metadata) -> Self {
+        let mut tag = 0u8;
+
+        match meta.mode() {
+            od::EntryMode::FILE => tag |= 0b0000_0001,
+            od::EntryMode::DIR => tag |= 0b0000_0010,
+            _ => {}
+        }
+
+        let content_length = meta.content_length();
+
+        let cache_control = match meta.cache_control() {
+            Some(v) => {
+                tag |= 0b0000_0100;
+                v.to_owned()
+            }
+            None => String::new(),

Review Comment:
   > We can make `ffi::Metadata` store `&str` rather than `String` to avoid 
unnecessary construct about `String`.
   
   We don't need to polish here. We can copy and paste it directly here. We can 
optimize this part until we are certain that it affects our performance.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to