summaryrefslogtreecommitdiffstats
path: root/vendor/git2/src/buf.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-04 12:41:41 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-04 12:41:41 +0000
commit10ee2acdd26a7f1298c6f6d6b7af9b469fe29b87 (patch)
treebdffd5d80c26cf4a7a518281a204be1ace85b4c1 /vendor/git2/src/buf.rs
parentReleasing progress-linux version 1.70.0+dfsg1-9~progress7.99u1. (diff)
downloadrustc-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/git2/src/buf.rs')
-rw-r--r--vendor/git2/src/buf.rs71
1 files changed, 71 insertions, 0 deletions
diff --git a/vendor/git2/src/buf.rs b/vendor/git2/src/buf.rs
new file mode 100644
index 000000000..fd2bcbf96
--- /dev/null
+++ b/vendor/git2/src/buf.rs
@@ -0,0 +1,71 @@
+use std::ops::{Deref, DerefMut};
+use std::ptr;
+use std::slice;
+use std::str;
+
+use crate::raw;
+use crate::util::Binding;
+
+/// A structure to wrap an intermediate buffer used by libgit2.
+///
+/// A buffer can be thought of a `Vec<u8>`, but the `Vec` type is not used to
+/// avoid copying data back and forth.
+pub struct Buf {
+ raw: raw::git_buf,
+}
+
+impl Default for Buf {
+ fn default() -> Self {
+ Self::new()
+ }
+}
+
+impl Buf {
+ /// Creates a new empty buffer.
+ pub fn new() -> Buf {
+ crate::init();
+ unsafe {
+ Binding::from_raw(&mut raw::git_buf {
+ ptr: ptr::null_mut(),
+ size: 0,
+ reserved: 0,
+ } as *mut _)
+ }
+ }
+
+ /// Attempt to view this buffer as a string slice.
+ ///
+ /// Returns `None` if the buffer is not valid utf-8.
+ pub fn as_str(&self) -> Option<&str> {
+ str::from_utf8(&**self).ok()
+ }
+}
+
+impl Deref for Buf {
+ type Target = [u8];
+ fn deref(&self) -> &[u8] {
+ unsafe { slice::from_raw_parts(self.raw.ptr as *const u8, self.raw.size as usize) }
+ }
+}
+
+impl DerefMut for Buf {
+ fn deref_mut(&mut self) -> &mut [u8] {
+ unsafe { slice::from_raw_parts_mut(self.raw.ptr as *mut u8, self.raw.size as usize) }
+ }
+}
+
+impl Binding for Buf {
+ type Raw = *mut raw::git_buf;
+ unsafe fn from_raw(raw: *mut raw::git_buf) -> Buf {
+ Buf { raw: *raw }
+ }
+ fn raw(&self) -> *mut raw::git_buf {
+ &self.raw as *const _ as *mut _
+ }
+}
+
+impl Drop for Buf {
+ fn drop(&mut self) {
+ unsafe { raw::git_buf_dispose(&mut self.raw) }
+ }
+}