Ref

Struct Ref 

Source
pub struct Ref<'a> { /* private fields */ }
Expand description

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.

Implementations§

Source§

impl<'a> Ref<'a>

Source

pub fn span(&self) -> Span

Returns the span of the referenced value.

Source

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

Returns a reference to the contained value.

Source

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

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.

Source

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

Returns an iterator over the elements of a vector.

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

Source

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

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>§

Source

pub fn is_list(&self) -> bool

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

Source

pub fn is_dotted_list(&self) -> bool

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());
Source

pub fn is_string(&self) -> bool

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());
Source

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

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());
Source

pub fn is_symbol(&self) -> bool

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());
Source

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

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

let v = sexp!(foo);

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

pub fn is_keyword(&self) -> bool

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());
Source

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

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

let v = sexp!(#:foo);

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

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

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"));
Source

pub fn is_bytes(&self) -> bool

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());
Source

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

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);
Source

pub fn is_number(&self) -> bool

Return true if the value is a number.

Source

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

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

Source

pub fn is_i64(&self) -> bool

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());
Source

pub fn is_u64(&self) -> bool

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());
Source

pub fn is_f64(&self) -> bool

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());
Source

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

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);
Source

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

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);
Source

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

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));
Source

pub fn is_boolean(&self) -> bool

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());
Source

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

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);
Source

pub fn is_char(&self) -> bool

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

Source

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

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);
Source

pub fn is_nil(&self) -> bool

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());
Source

pub fn as_nil(&self) -> Option<()>

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);
Source

pub fn is_null(&self) -> bool

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

Source

pub fn as_null(&self) -> Option<()>

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

Source

pub fn is_cons(&self) -> bool

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

Source

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

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

Source

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

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);
Source

pub fn is_vector(&self) -> bool

Returns true if the value is a vector.

Source

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

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));
Source

pub fn list_iter(&self) -> Option<ListIter<'_>>

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);
Source

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

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);
Source

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

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);
Source

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

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§

Source§

impl<'a> AsRef<Value> for Ref<'a>

Source§

fn as_ref(&self) -> &Value

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<'a> Clone for Ref<'a>

Source§

fn clone(&self) -> Ref<'a>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for Ref<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> Deref for Ref<'a>

Source§

type Target = Value

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<'a> From<Ref<'a>> for Datum

Source§

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

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

Source§

impl<'a> PartialEq for Ref<'a>

Source§

fn eq(&self, other: &Ref<'a>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a> Copy for Ref<'a>

Source§

impl<'a> StructuralPartialEq for Ref<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for Ref<'a>

§

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§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.