Skip to content

Commit cf225e4

Browse files
authored
Merge pull request #416 from wiktor-k/add-as-ref-to-maxbuffer
Implement `AsRef<[u8]>` for all buffer types
2 parents 52194ff + c05a14f commit cf225e4

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

tss-esapi/src/interface_types/algorithm.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,8 @@ impl TryFrom<TPMI_ALG_SYM_MODE> for SymmetricMode {
292292
/// Enum representing the asymmetric algorithm interface type.
293293
///
294294
/// # Details
295-
/// Use [AsymmetricAlgorithmSelection] instead where possible.
296-
/// This corresponds to TPMI_ALG_ASYM.
295+
/// Use [crate::abstraction::AsymmetricAlgorithmSelection] instead where possible.
296+
/// This corresponds to `TPMI_ALG_ASYM`.
297297
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
298298
pub enum AsymmetricAlgorithm {
299299
Rsa,

tss-esapi/src/structures/buffers.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ macro_rules! named_field_buffer_type {
4444
}
4545
}
4646

47+
impl AsRef<[u8]> for $native_type {
48+
fn as_ref(&self) -> &[u8] {
49+
self.as_bytes()
50+
}
51+
}
52+
4753
impl Deref for $native_type {
4854
type Target = Vec<u8>;
4955
fn deref(&self) -> &Self::Target {

tss-esapi/tests/integration_tests/structures_tests/buffers_tests/max_buffer_tests.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,17 @@ mod test_auth {
4747
assert_eq!(expected, actual);
4848
}
4949
}
50+
51+
#[test]
52+
fn test_as_ref() {
53+
// The following function accepts the broadest selection of types as its argument
54+
// including MaxBuffer. It also returns the MaxBuffer but in such a way that the
55+
// client is not aware of the true underlying type.
56+
fn accepts_returns_as_ref(data: impl AsRef<[u8]>) -> tss_esapi::Result<impl AsRef<[u8]>> {
57+
let data = data.as_ref();
58+
let max_buffer = MaxBuffer::from_bytes(data)?;
59+
Ok(max_buffer)
60+
}
61+
accepts_returns_as_ref(MaxBuffer::from_bytes(&[1, 2, 3]).unwrap()).unwrap();
62+
}
5063
}

0 commit comments

Comments
 (0)