Skip to content
Permalink
Browse files

Add Quark type

  • Loading branch information...
GuillaumeGomez committed Jul 28, 2018
1 parent a45a497 commit 83d80016b91bb441fd0ac11901e44942801347ed
Showing with 52 additions and 0 deletions.
  1. +2 −0 src/lib.rs
  2. +50 −0 src/quark.rs
@@ -202,6 +202,8 @@ mod value_array;
pub use value_array::ValueArray;
mod param_spec;
pub use param_spec::ParamSpec;
mod quark;
pub use quark::Quark;

#[cfg(feature="futures")]
mod main_context_futures;
@@ -0,0 +1,50 @@
// Copyright 2018, The Gtk-rs Project Developers.
// See the COPYRIGHT file at the top-level directory of this distribution.
// Licensed under the MIT license, see the LICENSE file or <http://opensource.org/licenses/MIT>

use ffi;
use translate::*;

use std::ffi::CStr;

#[repr(C)]
pub struct Quark(ffi::GQuark);

impl Quark {
pub fn from_string(s: &str) -> Quark {
unsafe {
from_glib(ffi::g_quark_from_static_string(s.to_glib_full()))
}
}

pub fn to_string(&self) -> &'static str {
unsafe {
CStr::from_ptr(ffi::g_quark_to_string(self.to_glib())).to_str().unwrap()
}
}

pub fn try_string(s: &str) -> Option<Quark> {
unsafe {
match ffi::g_quark_try_string(s.to_glib_none().0) {
0 => None,
x => Some(from_glib(x)),
}
}
}
}

#[doc(hidden)]
impl FromGlib<ffi::GQuark> for Quark {
fn from_glib(value: ffi::GQuark) -> Self {
Quark(value)
}
}

#[doc(hidden)]
impl ToGlib for Quark {
type GlibType = ffi::GQuark;

fn to_glib(&self) -> ffi::GQuark {
self.0
}
}

0 comments on commit 83d8001

Please sign in to comment.
You can’t perform that action at this time.