Skip to content

Commit

Permalink
ServerListRequest methods Option -> Result
Browse files Browse the repository at this point in the history
Result is #[must_use] unlike Option, so it is better to use Result in this case
  • Loading branch information
ymo-4 committed Jul 28, 2024
1 parent 5535258 commit c2aa1e5
Showing 1 changed file with 23 additions and 23 deletions.
46 changes: 23 additions & 23 deletions src/matchmaking_servers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,13 @@ macro_rules! gen_server_list_fn {
///
/// # Errors
///
/// Every filter's key and value must take 255 bytes or under, otherwise `None` is returned.
/// Every filter's key and value must take 255 bytes or under, otherwise `Err` is returned.
pub fn $name<ID: Into<AppId>>(
&self,
app_id: ID,
filters: &HashMap<&str, &str>,
callbacks: ServerListCallbacks,
) -> Option<Arc<Mutex<ServerListRequest>>> {
) -> Result<Arc<Mutex<ServerListRequest>>, ()> {
let app_id = app_id.into().0;
let mut filters = {
let mut vec = Vec::with_capacity(filters.len());
Expand All @@ -120,7 +120,7 @@ macro_rules! gen_server_list_fn {

// Max length is 255, so 256th byte will always be nul-terminator
if key_bytes.len() >= 256 || value_bytes.len() >= 256 {
return None;
return Err(());
}

let mut key = [0i8; 256];
Expand Down Expand Up @@ -164,7 +164,7 @@ macro_rules! gen_server_list_fn {

drop(request);

Some(request_arc)
Ok(request_arc)
}
}
};
Expand Down Expand Up @@ -360,31 +360,31 @@ impl ServerListRequest {
}
}

fn released(&self) -> Option<()> {
fn released(&self) -> Result<(), ()> {
if self.released {
None
Err(())
} else {
Some(())
Ok(())
}
}

/// # Errors
///
/// None if called on the released request
pub fn get_server_count(&self) -> Option<i32> {
/// Err if called on the released request
pub fn get_server_count(&self) -> Result<i32, ()> {
unsafe {
self.released()?;

Some(sys::SteamAPI_ISteamMatchmakingServers_GetServerCount(
Ok(sys::SteamAPI_ISteamMatchmakingServers_GetServerCount(
self.mms, self.h_req,
))
}
}

/// # Errors
///
/// None if called on the released request
pub fn get_server_details(&self, server: i32) -> Option<GameServerItem> {
/// Err if called on the released request
pub fn get_server_details(&self, server: i32) -> Result<GameServerItem, ()> {
unsafe {
self.released()?;

Expand All @@ -393,44 +393,44 @@ impl ServerListRequest {
self.mms, self.h_req, server,
);

Some(GameServerItem::from_ptr(server_item))
Ok(GameServerItem::from_ptr(server_item))
}
}

/// # Errors
///
/// None if called on the released request
pub fn refresh_query(&self) -> Option<()> {
/// Err if called on the released request
pub fn refresh_query(&self) -> Result<(), ()> {
unsafe {
self.released()?;

sys::SteamAPI_ISteamMatchmakingServers_RefreshQuery(self.mms, self.h_req);

Some(())
Ok(())
}
}

/// # Errors
///
/// None if called on the released request
pub fn refresh_server(&self, server: i32) -> Option<()> {
/// Err if called on the released request
pub fn refresh_server(&self, server: i32) -> Result<(), ()> {
unsafe {
self.released()?;

sys::SteamAPI_ISteamMatchmakingServers_RefreshServer(self.mms, self.h_req, server);

Some(())
Ok(())
}
}

/// # Errors
///
/// None if called on the released request
pub fn is_refreshing(&self) -> Option<bool> {
/// Err if called on the released request
pub fn is_refreshing(&self) -> Result<bool, ()> {
unsafe {
self.released()?;

Some(sys::SteamAPI_ISteamMatchmakingServers_IsRefreshing(
Ok(sys::SteamAPI_ISteamMatchmakingServers_IsRefreshing(
self.mms, self.h_req,
))
}
Expand Down Expand Up @@ -559,7 +559,7 @@ fn test_internet_servers() {
*data2.lock().unwrap() += 1;
}),
Box::new(move |list, _response| {
list.lock().unwrap().release();
list.lock().unwrap().release().unwrap();
println!("{}", data3.lock().unwrap());
}),
);
Expand Down

0 comments on commit c2aa1e5

Please sign in to comment.