aboutsummaryrefslogtreecommitdiffstats
path: root/components/util/atom.rs
blob: 49cb047768e5cda87f50a1868093f02e0fc8e6f0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

//! Provides a wrapper around the Atom type in the string cache
//! crate. It's needed so that it can implement the Encodable
//! trait which is required by Servo.

use serialize::{Encoder, Encodable};
use std::fmt;
use std::hash::Hash;
use string_cache::atom;

#[deriving(Clone, Eq, Hash, PartialEq)]
pub struct Atom {
    atom: atom::Atom,
}

impl Atom {
    #[inline(always)]
    pub fn from_slice(slice: &str) -> Atom {
        Atom {
            atom: atom::Atom::from_slice(slice)
        }
    }

    #[inline(always)]
    pub fn as_slice<'t>(&'t self) -> &'t str {
        self.atom.as_slice()
    }
}

impl fmt::Show for Atom {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:s}", self.atom.as_slice())
    }
}

impl<E, S: Encoder<E>> Encodable<S, E> for Atom {
    fn encode(&self, _s: &mut S) -> Result<(), E> {
        Ok(())
    }
}