rust_macios/foundation/
ns_range.rs1use std::ops::Range;
2
3use objc::Encoding;
4
5use crate::objective_c_runtime;
6
7use super::{NSString, UInt};
8
9#[derive(Debug, PartialEq, Eq, Clone, Copy, Default)]
11#[repr(C)]
12pub struct NSRange {
13 pub location: UInt,
15 pub length: UInt,
17}
18
19unsafe impl objective_c_runtime::Encode for NSRange {
20 fn encode() -> objc::Encoding {
21 unsafe { Encoding::from_str("{location=I, length=I}") }
22 }
23}
24
25extern "C" {
26 pub fn NSMakeRange(loc: UInt, len: UInt) -> NSRange;
28
29 pub fn NSMaxRange(range: NSRange) -> UInt;
31
32 pub fn NSIntersectionRange(range1: NSRange, range2: NSRange) -> NSRange;
34
35 pub fn NSUnionRange(range1: NSRange, range2: NSRange) -> NSRange;
37
38 pub fn NSLocationInRange(loc: UInt, range: NSRange) -> bool;
40
41 pub fn NSEqualRanges(range1: NSRange, range2: NSRange) -> bool;
43
44 pub fn NSRangeFromString(aString: NSString) -> NSRange;
46
47 pub fn NSStringFromRange(range: NSRange) -> NSString;
49}
50
51impl From<Range<usize>> for NSRange {
52 fn from(range: Range<usize>) -> Self {
53 NSRange {
54 location: range.start as UInt,
55 length: (range.end - range.start) as UInt,
56 }
57 }
58}
59
60impl From<NSRange> for Range<usize> {
61 fn from(range: NSRange) -> Self {
62 range.location as usize..(range.location + range.length) as usize
63 }
64}