[][src]Struct lexpr::datum::Ref

pub struct Ref<'a> { /* fields omitted */ }

A reference to a value and corresponding location information.

A Ref is the generalized version of &Datum; it can not only refer a top-level, owned Datum value, but also to values recursively contained therein.

Methods

impl<'a> Ref<'a>[src]

pub fn span(&self) -> Span[src]

Returns the span of the referenced value.

pub fn value(&self) -> &'a Value[src]

Returns a reference to the contained value.

pub fn list_iter(&self) -> Option<ListIter<'a>>[src]

If the value referenced is not either a cons cell or Null, None is returned.

Note that the returned iterator has special behavior for improper lists, yielding the element after the dot after returning None the first time; see Datum::list_iter for an example.

pub fn vector_iter(&self) -> Option<VectorIter<'a>>[src]

Returns an iterator over the elements of a vector.

If the value referenced is not a vector, None is returned.

pub fn as_pair(&self) -> Option<(Ref<'a>, Ref<'a>)>[src]

Returns a pair of references to the fields of a cons cell.

If the value referenced is not a cons cell, None is returned.

Methods from Deref<Target = Value>

pub fn is_list(&self) -> bool[src]

Returns true if the value is a (proper) list.

pub fn is_dotted_list(&self) -> bool[src]

Returns true if the value is a dotted (improper) list.

Note that all values that are not pairs are considered dotted lists.

let list = sexp!((1 2 3));
assert!(!list.is_dotted_list());
let dotted = sexp!((1 2 . 3));
assert!(dotted.is_dotted_list());

pub fn is_string(&self) -> bool[src]

Returns true if the value is a String. Returns false otherwise.

For any Value on which is_string returns true, as_str is guaranteed to return the string slice.

let v = sexp!(((a . "some string") (b . #f)));

assert!(v["a"].is_string());

// The boolean `false` is not a string.
assert!(!v["b"].is_string());

pub fn as_str(&self) -> Option<&str>[src]

If the value is a String, returns the associated str. Returns None otherwise.

let v = sexp!(((a . "some string") (b . #f)));

assert_eq!(v["a"].as_str(), Some("some string"));

// The boolean `false` is not a string.
assert_eq!(v["b"].as_str(), None);

// S-expression values are printed in S-expression
// representation, so strings are in quotes.
//    The value is: "some string"
println!("The value is: {}", v["a"]);

// Rust strings are printed without quotes.
//
//    The value is: some string
println!("The value is: {}", v["a"].as_str().unwrap());

pub fn is_symbol(&self) -> bool[src]

Returns true if the value is a symbol. Returns false otherwise.

For any Value on which is_symbol returns true, as_symbol is guaranteed to return the string slice.

let v = sexp!((#:foo bar "baz"));

assert!(v[1].is_symbol());

// Keywords and strings are not symbols.
assert!(!v[0].is_symbol());
assert!(!v[2].is_symbol());

pub fn as_symbol(&self) -> Option<&str>[src]

If the value is a symbol, returns the associated str. Returns None otherwise.

let v = sexp!(foo);

assert_eq!(v.as_symbol(), Some("foo"));

pub fn is_keyword(&self) -> bool[src]

Returns true if the value is a keyword. Returns false otherwise.

For any Value on which is_keyword returns true, as_keyword is guaranteed to return the string slice.

let v = sexp!((#:foo bar "baz"));

assert!(v[0].is_keyword());

// Symbols and strings are not keywords.
assert!(!v[1].is_keyword());
assert!(!v[2].is_keyword());

pub fn as_keyword(&self) -> Option<&str>[src]

If the value is a keyword, returns the associated str. Returns None otherwise.

let v = sexp!(#:foo);

assert_eq!(v.as_keyword(), Some("foo"));

pub fn as_name(&self) -> Option<&str>[src]

Get the name of a symbol or keyword, or the value of a string.

This is useful if symbols, keywords and strings need to be treated equivalently in some context.

let kw = sexp!(#:foo);
assert_eq!(kw.as_name(), Some("foo"));

let sym = sexp!(bar);
assert_eq!(sym.as_name(), Some("bar"));

let s = sexp!("baz");
assert_eq!(s.as_name(), Some("baz"));

pub fn is_bytes(&self) -> bool[src]

Returns true if the value is a byte vector. Returns false otherwise.

For any Value on which is_bytes returns true, as_bytes is guaranteed to return the byte slice.

let v = sexp!(((a . ,(b"some bytes" as &[u8])) (b . "string")));

assert!(v["a"].is_bytes());

// A string is not a byte vector.
assert!(!v["b"].is_bytes());

pub fn as_bytes(&self) -> Option<&[u8]>[src]

If the value is a byte vector, returns the associated byte slice. Returns None otherwise.

let v = sexp!(((a . ,(b"some bytes" as &[u8])) (b . "string")));

assert_eq!(v["a"].as_bytes(), Some(b"some bytes" as &[u8]));

// A string is not a byte vector.
assert_eq!(v["b"].as_bytes(), None);

pub fn is_number(&self) -> bool[src]

Return true if the value is a number.

pub fn as_number(&self) -> Option<&Number>[src]

For numbers, return a reference to them. For other values, return None.

pub fn is_i64(&self) -> bool[src]

Returns true if the value is an integer between i64::MIN and i64::MAX.

For any Value on which is_i64 returns true, as_i64 is guaranteed to return the integer value.

let big = i64::max_value() as u64 + 10;
let v = sexp!(((a . 64) (b . ,big) (c . 256.0)));

assert!(v["a"].is_i64());

// Greater than i64::MAX.
assert!(!v["b"].is_i64());

// Numbers with a decimal point are not considered integers.
assert!(!v["c"].is_i64());

pub fn is_u64(&self) -> bool[src]

Returns true if the value is an integer between zero and u64::MAX.

For any Value on which is_u64 returns true, as_u64 is guaranteed to return the integer value.

let v = sexp!(((a . 64) (b . -64) (c . 256.0)));

assert!(v["a"].is_u64());

// Negative integer.
assert!(!v["b"].is_u64());

// Numbers with a decimal point are not considered integers.
assert!(!v["c"].is_u64());

pub fn is_f64(&self) -> bool[src]

Returns true if the value is a number that can be represented by f64.

For any Value on which is_f64 returns true, as_f64 is guaranteed to return the floating point value.

Currently this function returns true if and only if both is_i64 and is_u64 return false but this is not a guarantee in the future.

let v = sexp!(((a . 256.0) (b . 64) (c . -64)));

assert!(v["a"].is_f64());

// Integers.
assert!(!v["b"].is_f64());
assert!(!v["c"].is_f64());

pub fn as_i64(&self) -> Option<i64>[src]

If the value is an integer, represent it as i64 if possible. Returns None otherwise.

let big = i64::max_value() as u64 + 10;
let v = sexp!(((a . 64) (b . ,big) (c . 256.0)));

assert_eq!(v["a"].as_i64(), Some(64));
assert_eq!(v["b"].as_i64(), None);
assert_eq!(v["c"].as_i64(), None);

pub fn as_u64(&self) -> Option<u64>[src]

If the value is an integer, represent it as u64 if possible. Returns None otherwise.

let v = sexp!(((a . 64) (b . -64) (c . 256.0)));

assert_eq!(v["a"].as_u64(), Some(64));
assert_eq!(v["b"].as_u64(), None);
assert_eq!(v["c"].as_u64(), None);

pub fn as_f64(&self) -> Option<f64>[src]

If the value is a number, represent it as f64 if possible. Returns None otherwise.

let v = sexp!(((a . 256.0) (b . 64) (c . -64)));

assert_eq!(v["a"].as_f64(), Some(256.0));
assert_eq!(v["b"].as_f64(), Some(64.0));
assert_eq!(v["c"].as_f64(), Some(-64.0));

pub fn is_boolean(&self) -> bool[src]

Returns true if the value is a Boolean. Returns false otherwise.

For any Value on which is_boolean returns true, as_bool is guaranteed to return the boolean value.

let v = sexp!(((a . #f) (b . #nil)));

assert!(v["a"].is_boolean());

// The nil value is special, and not a boolean.
assert!(!v["b"].is_boolean());

pub fn as_bool(&self) -> Option<bool>[src]

If the value is a Boolean, returns the associated bool. Returns None otherwise.

let v = sexp!(((a . #f) (b . "false")));

assert_eq!(v["a"].as_bool(), Some(false));

// The string `"false"` is a string, not a boolean.
assert_eq!(v["b"].as_bool(), None);

pub fn is_char(&self) -> bool[src]

Returns true if the value is a character. Returns false otherwise.

pub fn as_char(&self) -> Option<char>[src]

If the value is a character, returns the associated char. Returns None otherwise.

let v = sexp!(((a . 'c') (b . "c")));

assert_eq!(v["a"].as_char(), Some('c'));

// The string `"c"` is a single-character string, not a character.
assert_eq!(v["b"].as_char(), None);

pub fn is_nil(&self) -> bool[src]

Returns true if the value is Nil. Returns false otherwise.

For any Value on which is_nil returns true, as_nil is guaranteed to return Some(()).

let v = sexp!(((a . #nil) (b . #f)));

assert!(v["a"].is_nil());

// The boolean `false` is not nil.
assert!(!v["b"].is_nil());

pub fn as_nil(&self) -> Option<()>[src]

If the value is Nil, returns (). Returns None otherwise.

let v = sexp!(((a . #nil) (b . #f) (c . ())));

assert_eq!(v["a"].as_nil(), Some(()));

// The boolean `false` is not nil.
assert_eq!(v["b"].as_nil(), None);
// Neither is the empty list.
assert_eq!(v["c"].as_nil(), None);

pub fn is_null(&self) -> bool[src]

Returns true if the value is Null. Returns false otherwise.

pub fn as_null(&self) -> Option<()>[src]

If the value is Null, returns (). Returns None otherwise.

pub fn is_cons(&self) -> bool[src]

Returns true if the value is a cons cell. Returns False otherwise.

pub fn as_cons(&self) -> Option<&Cons>[src]

If the value is a cons cell, returns a reference to it. Returns None otherwise.

pub fn as_pair(&self) -> Option<(&Value, &Value)>[src]

If the value is a cons cell, return references to its car and cdr fields.

let cell = sexp!((foo . bar));
assert_eq!(cell.as_pair(), Some((&sexp!(foo), &sexp!(bar))));
assert_eq!(sexp!("not-a-pair").as_pair(), None);

pub fn is_vector(&self) -> bool[src]

Returns true if the value is a vector.

pub fn as_slice(&self) -> Option<&[Value]>[src]

If the value is a vector, return a reference to its elements.

let v = sexp!(#(1 2 "three"));
let slice: &[Value] = &[sexp!(1), sexp!(2), sexp!("three")];
assert_eq!(v.as_slice(), Some(slice));

pub fn list_iter(&self) -> Option<ListIter>[src]

If the value is a list, return an iterator over the list elements.

If the value is not either a cons cell or Null, None is returned. Note that the returned iterator has special behavior for improper lists, yielding the element after the dot after returning None the first time.

use lexpr::sexp;

let value = lexpr::from_str("(1 2 . 3)").unwrap();
let mut iter = value.list_iter().unwrap();
assert_eq!(iter.next(), Some(&sexp!(1)));
assert_eq!(iter.next(), Some(&sexp!(2)));
assert_eq!(iter.next(), None);
assert_eq!(iter.next(), Some(&sexp!(3)));
assert_eq!(iter.next(), None);

pub fn to_vec(&self) -> Option<Vec<Value>>[src]

Attempts conversion to a vector, cloning the values.

For proper lists (including Value::Null), this returns a vector of values. If you want to handle improper list in a similar way, combine as_cons and the Cons::to_vec method.

assert_eq!(sexp!((1 2 3)).to_vec(), Some(vec![sexp!(1), sexp!(2), sexp!(3)]));
assert_eq!(sexp!(()).to_vec(), Some(vec![]));
assert_eq!(sexp!((1 2 . 3)).to_vec(), None);

pub fn to_ref_vec(&self) -> Option<Vec<&Value>>[src]

Attempts conversion to a vector, taking references to the values.

For proper lists (including Value::Null), this returns a vector of value references. If you want to handle improper list in a similar way, combine as_cons and the Cons::to_ref_vec method.

assert_eq!(sexp!((1 2 3)).to_ref_vec(), Some(vec![&sexp!(1), &sexp!(2), &sexp!(3)]));
assert_eq!(sexp!(()).to_ref_vec(), Some(vec![]));
assert_eq!(sexp!((1 2 . 3)).to_ref_vec(), None);

pub fn get<I: Index>(&self, index: I) -> Option<&Value>[src]

Index into a S-expression list. A string or Value value can be used to access a value in an association list, and a usize index can be used to access the n-th element of a list.

For indexing into association lists, the given string will match strings, symbols and keywords.

Returns None if the type of self does not match the type of the index, for example if the index is a string and self is not an association list. Also returns None if the given key does not exist in the map or the given index is not within the bounds of the list; note that the tail of an improper list is also considered out-of-bounds.

In Scheme terms, this method can be thought of a combination of assoc-ref and list-ref, depending on the argument type. If you want to look up a number in an association list, use an Value value containing that number.

let alist = sexp!((("A" . 65) (B . 66) (#:C . 67) (42 . "The answer")));
assert_eq!(alist.get("A").unwrap(), &sexp!(65));
assert_eq!(alist.get("B").unwrap(), &sexp!(66));
assert_eq!(alist.get("C").unwrap(), &sexp!(67));
assert_eq!(alist.get(sexp!(42)).unwrap(), &sexp!("The answer"));

let list = sexp!(("A" "B" "C"));
assert_eq!(*list.get(2).unwrap(), sexp!("C"));

assert_eq!(list.get("A"), None);

Square brackets can also be used to index into a value in a more concise way. This returns the nil value in cases where get would have returned None. See Index for details.

let alist = sexp!((
    ("A" . ("a" "á" "à"))
    ("B" . ((b . 42) (c . 23)))
    ("C" . ("c" "ć" "ć̣" "ḉ"))
));
assert_eq!(alist["B"][0], sexp!((b . 42)));
assert_eq!(alist["C"][1], sexp!("ć"));

assert_eq!(alist["D"], sexp!(#nil));
assert_eq!(alist[0]["x"]["y"]["z"], sexp!(#nil));

Trait Implementations

impl<'a> AsRef<Value> for Ref<'a>[src]

impl<'a> Clone for Ref<'a>[src]

impl<'a> Copy for Ref<'a>[src]

impl<'a> Debug for Ref<'a>[src]

impl<'a> Deref for Ref<'a>[src]

type Target = Value

The resulting type after dereferencing.

impl<'a> From<Ref<'a>> for Datum[src]

fn from(r: Ref<'a>) -> Self[src]

Turns a reference into an owned Datum, by cloning the referenced value and location information.

impl<'a> PartialEq<Ref<'a>> for Ref<'a>[src]

impl<'a> StructuralPartialEq for Ref<'a>[src]

Auto Trait Implementations

impl<'a> RefUnwindSafe for Ref<'a>

impl<'a> Send for Ref<'a>

impl<'a> Sync for Ref<'a>

impl<'a> Unpin for Ref<'a>

impl<'a> UnwindSafe for Ref<'a>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.