Skip to main content

Attributes

Struct Attributes 

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

The attributes of one node or edge, name to value, in the order they were put: the map that the hooks of DotWriter::node_attributes and DotWriter::edge_attributes fill.

The attributes, such as color or shape, are those of DOT (the language of Graphviz). Java’s AttributeHook is a closure Fn(u32, &mut Attributes<'a>) here, called with the node ID or edge index and this map. The writer passes it holding the label from node_label or edge_label as the attribute label, if there is one, and nothing else; the hook can add attributes, and replace or remove the label. The writer writes the attributes in the order they were put and escapes their values; a name that is not an identifier or is a DOT keyword makes write panic with an invalid argument message. The writer reuses the map for every node and edge, so a hook cannot keep it: the borrow ends with the call.

use graphity_core::GraphBuilder;
use graphity_dot::DotWriter;

let mut b = GraphBuilder::new();
b.add_nodes(2);
b.add_edge(0, 1);
let graph = b.build();

let dot = DotWriter::of(&graph)
    .node_label(|node| Some(format!("v{node}").into()))
    .node_attributes(|node, attributes| {
        if node == 0 {
            attributes.insert("shape", "box");
            attributes.insert("label", "entry"); // replaces the label, keeps its place
        }
    })
    .write_to_string();
assert!(dot.contains("  0 [label=\"entry\", shape=\"box\"];\n"));

Names and values are Cows: a string literal or a &str into the caller’s data, which lives for the writer’s lifetime 'a, is kept without a copy, and a computed String is moved in.

Putting a name that is present replaces its value in place, as Java’s LinkedHashMap.put does; removing an entry keeps the order of the others. Lookups search the entries linearly: an element carries a handful of attributes, for which a vector of pairs is faster than hashing.

Implementations§

Source§

impl<'a> Attributes<'a>

Source

pub fn new() -> Self

Returns an empty map.

Time: O(1).

Source

pub fn insert( &mut self, name: impl Into<Cow<'a, str>>, value: impl Into<Cow<'a, str>>, ) -> Option<Cow<'a, str>>

Sets the value of name and returns the previous one - Java’s Map.put. A new name is appended; a present one keeps its place.

Time: O(len).

Source

pub fn get(&self, name: &str) -> Option<&str>

Returns the value of name, or None if it is absent.

Time: O(len).

Source

pub fn contains_key(&self, name: &str) -> bool

Returns whether name is present.

Time: O(len).

Source

pub fn remove(&mut self, name: &str) -> Option<Cow<'a, str>>

Removes name and returns its value, or None if it is absent; the other entries keep their order.

Time: O(len).

Source

pub fn len(&self) -> usize

Returns the number of entries.

Time: O(1).

Source

pub fn is_empty(&self) -> bool

Returns whether the map has no entries.

Time: O(1).

Source

pub fn clear(&mut self)

Removes every entry.

Time: O(len).

Source

pub fn iter(&self) -> Iter<'_> ⓘ

Returns the entries as (name, value), in the order they were put.

Time: O(1) to create the iterator.

Trait Implementations§

Source§

impl<'a> Clone for Attributes<'a>

Source§

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

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for Attributes<'a>

Source§

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

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

impl<'a> Default for Attributes<'a>

Source§

fn default() -> Attributes<'a>

Returns the “default value” for a type. Read more
Source§

impl<'a> Eq for Attributes<'a>

Source§

impl<'b> IntoIterator for &'b Attributes<'_>

Source§

type Item = (&'b str, &'b str)

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'b>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Iter<'b> ⓘ

Creates an iterator from a value. Read more
Source§

impl<'a> PartialEq for Attributes<'a>

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl<'a> StructuralPartialEq for Attributes<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for Attributes<'a>

§

impl<'a> RefUnwindSafe for Attributes<'a>

§

impl<'a> Send for Attributes<'a>

§

impl<'a> Sync for Attributes<'a>

§

impl<'a> Unpin for Attributes<'a>

§

impl<'a> UnsafeUnpin for Attributes<'a>

§

impl<'a> UnwindSafe for Attributes<'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<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.