mac_util.mm 711 B

1234567891011121314151617181920
  1. // Copyright (c) 2024 Microsoft, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #import <Cocoa/Cocoa.h>
  5. #include "base/containers/span.h"
  6. namespace electron::util {
  7. base::span<const uint8_t> as_byte_span(NSData* data) {
  8. // SAFETY: There is no NSData API that passes the UNSAFE_BUFFER_USAGE
  9. // test, so let's isolate the unsafe API use into this function. Instead of
  10. // calling '[data bytes]' and '[data length]' directly, the rest of our
  11. // code should prefer to use spans returned by this function.
  12. return UNSAFE_BUFFERS(base::span{
  13. reinterpret_cast<const uint8_t*>([data bytes]), [data length]});
  14. }
  15. } // namespace electron::util