sl4f_lib/common_utils/buffer.rs
1// Copyright 2021 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use fidl_fuchsia_mem as fmem;
6
7pub fn try_from_bytes(value: &[u8]) -> Result<fmem::Buffer, zx::Status> {
8 let size = value.len() as u64;
9 let vmo = zx::Vmo::create(size)?;
10 vmo.write(value, 0)?;
11 Ok(fmem::Buffer { vmo, size })
12}
13
14pub fn try_into_bytes(buffer: fmem::Buffer) -> Result<Vec<u8>, zx::Status> {
15 let mut data: Vec<u8> = vec![0; buffer.size as usize];
16 buffer.vmo.read(data.as_mut_slice(), 0)?;
17 Ok(data)
18}