Debian Codegraph @

third_party/rust/libudev/src/context.rs

raw
1 use std::path::Path;
2
3 use ::device::{Device};
4 use ::handle::{Handle};
5
6 /// A libudev context.
7 pub struct Context {
8 udev: *mut ::ffi::udev
9 }
10
11 impl Drop for Context {
12 fn drop(&mut self) {
13 unsafe {
14 ::ffi::udev_unref(self.udev);
15 }
16 }
17 }
18
19 #[doc(hidden)]
20 impl Handle<::ffi::udev> for Context {
21 fn as_ptr(&self) -> *mut ::ffi::udev {
22 self.udev
23 }
24 }
25
26 impl Context {
27 /// Creates a new context.
28 pub fn new() -> ::Result<Self> {
29 let ptr = try_alloc!(unsafe { ::ffi::udev_new() });
30
31 Ok(Context { udev: ptr })
32 }
33
34 /// Creates a device for a given syspath.
35 ///
36 /// The `syspath` parameter should be a path to the device file within the `sysfs` file system,
37 /// e.g., `/sys/devices/virtual/tty/tty0`.
38 pub fn device_from_syspath(&self, syspath: &Path) -> ::Result<Device> {
39 let syspath = try!(::util::os_str_to_cstring(syspath));
40
41 let ptr = try_alloc!(unsafe {
42 ::ffi::udev_device_new_from_syspath(self.udev, syspath.as_ptr())
43 });
44
45 Ok(::device::new(self, ptr))
46 }
47 }