Skip to content

Commit

Permalink
Drop get_ prefix from fns (#437)
Browse files Browse the repository at this point in the history
According to [Rust's C-GETTER convention] function names should not have
a `get_` prefix, and we maintain this standard in some but not all parts
of the `ndk`.  Consistenize that by dropping it everywhere, and add
`doc(alias)`es to at least these functions (as I have been doing to new
API wrappers added over the past few months too) to make it easier for
users to find by the original NDK C symbol name.

In addition the `audio` module received some extra doc cleanup to get
rid of redundant `Available since API level xx.` doc-comments (these
are already provided via `feature`s and `doc_cfg`) as well as vague
statements about integer return values when the return type has been
translated to a `Return<()>`.

[Rust's C-GETTER convention]: https://rust-lang.github.io/api-guidelines/naming.html#getter-names-follow-rust-convention-c-getter
  • Loading branch information
MarijnS95 authored Oct 10, 2023
1 parent 091764f commit b17576a
Show file tree
Hide file tree
Showing 6 changed files with 249 additions and 137 deletions.
1 change: 1 addition & 0 deletions ndk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
- **Breaking:** `raw-window-handle 0.5` support is now behind an _optional_ `rwh_05` crate feature and `raw-window-handle` `0.4` and `0.6` support is provided via the new `rwh_04` and (default-enabled) `rwh_06` crate features. (#434)
- **Breaking:** looper: Provide `event` value to file descriptor poll callback. (#435)
- **Breaking:** `HardwareBufferFormat` is no longer exported from `hardware_buffer` and `native_window`, and can only be reached through the `hardware_buffer_format` module. (#436)
- **Breaking:** `get_` prefixes have been removed from all public functions in light of the [C-GETTER](https://rust-lang.github.io/api-guidelines/naming.html#getter-names-follow-rust-convention-c-getter) convention. (#437)

# 0.7.0 (2022-07-24)

Expand Down
22 changes: 18 additions & 4 deletions ndk/src/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use std::{
///
/// [`AAssetManager *`]: https://developer.android.com/ndk/reference/group/asset#aassetmanager
#[derive(Debug)]
#[doc(alias = "AAssetManager")]
pub struct AssetManager {
ptr: NonNull<ffi::AAssetManager>,
}
Expand Down Expand Up @@ -42,6 +43,7 @@ impl AssetManager {
/// Open the asset. Returns [`None`] if opening the asset fails.
///
/// This currently always opens the asset in the streaming mode.
#[doc(alias = "AAssetManager_open")]
pub fn open(&self, filename: &CStr) -> Option<Asset> {
unsafe {
let ptr = ffi::AAssetManager_open(
Expand All @@ -54,6 +56,7 @@ impl AssetManager {
}

/// Open an asset directory. Returns [`None`] if opening the directory fails.
#[doc(alias = "AAssetManager_openDir")]
pub fn open_dir(&self, filename: &CStr) -> Option<AssetDir> {
unsafe {
let ptr = ffi::AAssetManager_openDir(self.ptr.as_ptr(), filename.as_ptr());
Expand Down Expand Up @@ -90,6 +93,7 @@ impl AssetManager {
///
/// [`AAssetDir *`]: https://developer.android.com/ndk/reference/group/asset#aassetdir
#[derive(Debug)]
#[doc(alias = "AAssetDir")]
pub struct AssetDir {
ptr: NonNull<ffi::AAssetDir>,
}
Expand All @@ -98,6 +102,7 @@ pub struct AssetDir {
// However, AAsset is not, so there's a good chance that AAssetDir is not either.

impl Drop for AssetDir {
#[doc(alias = "AAssetDir_close")]
fn drop(&mut self) {
unsafe { ffi::AAssetDir_close(self.ptr.as_ptr()) }
}
Expand All @@ -123,6 +128,7 @@ impl AssetDir {
/// no additional allocation.
///
/// The filenames are in the correct format to be passed to [`AssetManager::open()`].
#[doc(alias = "AAssetDir_getNextFileName")]
pub fn with_next<T>(&mut self, f: impl for<'a> FnOnce(&'a CStr) -> T) -> Option<T> {
unsafe {
let next_name = ffi::AAssetDir_getNextFileName(self.ptr.as_ptr());
Expand All @@ -135,6 +141,7 @@ impl AssetDir {
}

/// Reset the iteration state
#[doc(alias = "AAssetDir_rewind")]
pub fn rewind(&mut self) {
unsafe {
ffi::AAssetDir_rewind(self.ptr.as_ptr());
Expand Down Expand Up @@ -169,6 +176,7 @@ impl Iterator for AssetDir {
///
/// [`AAsset *`]: https://developer.android.com/ndk/reference/group/asset#aasset
#[derive(Debug)]
#[doc(alias = "AAsset")]
pub struct Asset {
ptr: NonNull<ffi::AAsset>,
}
Expand All @@ -177,6 +185,7 @@ pub struct Asset {
// See https://developer.android.com/ndk/reference/group/asset#aasset

impl Drop for Asset {
#[doc(alias = "AAsset_close")]
fn drop(&mut self) {
unsafe { ffi::AAsset_close(self.ptr.as_ptr()) }
}
Expand All @@ -200,17 +209,20 @@ impl Asset {
}

/// Returns the total length of the asset, in bytes
pub fn get_length(&self) -> usize {
#[doc(alias = "AAsset_getLength64")]
pub fn length(&self) -> usize {
unsafe { ffi::AAsset_getLength64(self.ptr.as_ptr()) as usize }
}

/// Returns the remaining length of the asset, in bytes
pub fn get_remaining_length(&self) -> usize {
#[doc(alias = "AAsset_getRemainingLength64")]
pub fn remaining_length(&self) -> usize {
unsafe { ffi::AAsset_getRemainingLength64(self.ptr.as_ptr()) as usize }
}

/// Maps all data into a buffer and returns it
pub fn get_buffer(&mut self) -> io::Result<&[u8]> {
#[doc(alias = "AAsset_getBuffer")]
pub fn buffer(&mut self) -> io::Result<&[u8]> {
unsafe {
let buf_ptr = ffi::AAsset_getBuffer(self.ptr.as_ptr());
if buf_ptr.is_null() {
Expand All @@ -221,7 +233,7 @@ impl Asset {
} else {
Ok(std::slice::from_raw_parts(
buf_ptr as *const u8,
self.get_length(),
self.length(),
))
}
}
Expand Down Expand Up @@ -258,6 +270,7 @@ impl Asset {
}

impl io::Read for Asset {
#[doc(alias = "AAsset_read")]
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
unsafe {
let res = ffi::AAsset_read(self.ptr.as_ptr(), buf.as_mut_ptr() as *mut _, buf.len());
Expand All @@ -274,6 +287,7 @@ impl io::Read for Asset {
}

impl io::Seek for Asset {
#[doc(alias = "AAsset_seek64")]
fn seek(&mut self, seek: io::SeekFrom) -> io::Result<u64> {
unsafe {
let res = match seek {
Expand Down
Loading

0 comments on commit b17576a

Please sign in to comment.