1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use {
    std::{
        future::Future,
        io::prelude::*,
        pin::Pin,
    },
    tokio::io::{
        AsyncRead,
        AsyncReadExt as _,
        AsyncWrite,
        AsyncWriteExt as _,
    },
    crate::{
        ErrorContext,
        Protocol,
        ReadError,
        ReadErrorKind,
        WriteError,
    },
};

/// A git object ID uses its native binary representation, a sequence of 20 bytes.
#[cfg_attr(docsrs, doc(cfg(feature = "git2")))]
impl Protocol for git2::Oid {
    fn read<'a, R: AsyncRead + Unpin + Send + 'a>(stream: &'a mut R) -> Pin<Box<dyn Future<Output = Result<Self, ReadError>> + Send + 'a>> {
        Box::pin(async move {
            let mut buf = [0; 20];
            stream.read_exact(&mut buf).await.map_err(|e| ReadError {
                context: ErrorContext::BuiltIn { for_type: "git2::Oid" },
                kind: e.into(),
            })?;
            Self::from_bytes(&buf).map_err(|e| ReadError {
                context: ErrorContext::BuiltIn { for_type: "git2::Oid" },
                kind: ReadErrorKind::Custom(e.to_string()),
            })
        })
    }

    fn write<'a, W: AsyncWrite + Unpin + Send + 'a>(&'a self, sink: &'a mut W) -> Pin<Box<dyn Future<Output = Result<(), WriteError>> + Send + 'a>> {
        Box::pin(async move {
            sink.write_all(self.as_bytes()).await.map_err(|e| WriteError {
                context: ErrorContext::BuiltIn { for_type: "git2::Oid" },
                kind: e.into(),
            })?;
            Ok(())
        })
    }

    fn read_sync(stream: &mut impl Read) -> Result<Self, ReadError> {
        let mut buf = [0; 20];
        stream.read_exact(&mut buf).map_err(|e| ReadError {
            context: ErrorContext::BuiltIn { for_type: "git2::Oid" },
            kind: e.into(),
        })?;
        Self::from_bytes(&buf).map_err(|e| ReadError {
            context: ErrorContext::BuiltIn { for_type: "git2::Oid" },
            kind: ReadErrorKind::Custom(e.to_string()),
        })
    }

    fn write_sync(&self, sink: &mut impl Write) -> Result<(), WriteError> {
        sink.write_all(self.as_bytes()).map_err(|e| WriteError {
            context: ErrorContext::BuiltIn { for_type: "git2::Oid" },
            kind: e.into(),
        })?;
        Ok(())
    }
}