Skip to content

Commit c05a14f

Browse files
committed
Add test illustrating how to use AsRef<[u8]> for MaxBuffer
This test shows how to hide the type being returned from a function that moves out MaxBuffer. This way clients can hide the implementation detail (MaxBuffer). Signed-off-by: Wiktor Kwapisiewicz <[email protected]>
1 parent 4a07f3f commit c05a14f

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

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)