wpilib_sys/usage.rs
1/* PORTIONS OF THIS FILE WERE ORIGINALLY DISTRIBUTED WITH THE FOLLOWING LICENSE
2
3"""
4MIT License
5Copyright (c) 2017 Rust for Robotics Developers
6Permission is hereby granted, free of charge, to any person obtaining a copy
7of this software and associated documentation files (the "Software"), to deal
8in the Software without restriction, including without limitation the rights
9to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10copies of the Software, and to permit persons to whom the Software is
11furnished to do so, subject to the following conditions:
12
13The above copyright notice and this permission notice shall be included in all
14copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22SOFTWARE.
23"""
24
25Copyright 2018 First Rust Competition Developers.
26Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
27http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
28<LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
29option. This file may not be copied, modified, or distributed
30except according to those terms.
31*/
32
33#![macro_use]
34use super::bindings::HAL_Report;
35use std::ffi::CStr;
36use std::ptr;
37
38pub use super::bindings::HALUsageReporting_tInstances as instances;
39pub use super::bindings::HALUsageReporting_tResourceType as resource_types;
40
41/// Report the usage of a specific resource type with an `instance` value attached.
42///
43/// This is provided as a utility for library developers.
44pub fn report_usage(resource: resource_types::Type, instance: instances::Type) -> i64 {
45 report_usage_context(resource, instance, 0)
46}
47
48/// Report usage of a resource with additional context.
49///
50/// This is provided as a utility for library developers.
51pub fn report_usage_context(
52 resource: resource_types::Type,
53 instance: instances::Type,
54 context: i32,
55) -> i64 {
56 unsafe { HAL_Report(resource as i32, instance as i32, context, ptr::null()) }
57}
58
59/// This is provided as a utility for library developers.
60/// Designed to be used with null-terminated byte string literals like `b"message\0"`
61///
62/// # Panics
63/// If the underlying byte slice is not null-terminated, the function will panic
64pub fn report_usage_extras<F: AsRef<[u8]>>(
65 resource: resource_types::Type,
66 instance: instances::Type,
67 context: i32,
68 feature: F,
69) -> i64 {
70 // local binding just to be safe with lifetimes, see https://doc.rust-lang.org/std/ffi/struct.CStr.html#method.as_ptr
71 let cstr = CStr::from_bytes_with_nul(feature.as_ref())
72 .expect("report_usage_extras features must be null-terminated!");
73 unsafe { HAL_Report(resource as i32, instance as i32, context, cstr.as_ptr()) }
74}