Skip to main content

user_copy/
user_string_view.rs

1// Copyright 2026 The Fuchsia Authors
2//
3// Use of this source code is governed by a MIT-style
4// license that can be found in the LICENSE file or at
5// https://opensource.org/licenses/MIT
6
7use crate::user_ptr::UserInPtr;
8use zx_status::Status;
9
10/// A wrapper around `zx_string_view_t` passed from userspace into kernel syscalls.
11///
12/// This struct has exact memory layout parity with `zx_string_view_t` (`const char*`, `size_t`),
13/// ensuring ABI compatibility when passed by value across FFI boundaries.
14#[repr(C)]
15#[derive(Debug, Copy, Clone, Default)]
16pub struct UserStringView {
17    /// Pointer to userspace UTF-8 bytes.
18    pub data: UserInPtr<u8>,
19    /// Length of the string in bytes.
20    pub length: usize,
21}
22
23impl UserStringView {
24    /// Returns true if the string view is empty.
25    pub fn is_empty(&self) -> bool {
26        self.length == 0
27    }
28
29    /// Returns the length of the string view in bytes.
30    pub fn len(&self) -> usize {
31        self.length
32    }
33
34    /// Copies the string bytes from userspace into a destination slice.
35    ///
36    /// Returns `Status::INVALID_ARGS` if the slice is smaller than `length`.
37    pub fn copy_slice_from_user<'a>(
38        &self,
39        dst: &'a mut [core::mem::MaybeUninit<u8>],
40    ) -> Result<&'a mut [u8], Status> {
41        if dst.len() < self.length {
42            return Err(Status::INVALID_ARGS);
43        }
44        self.data.copy_slice_from_user(&mut dst[..self.length])
45    }
46}