pub fn cas<K: AsRef<[u8]>>(
        &self,
        key: K,
        old: Option<&[u8]>,
        new: Option<Value>,
    ) -> Result<(), Option<PinnedValue>> {
        trace!("casing key {:?}", key.as_ref());
        let _measure = Measure::new(&M.tree_cas);

        if self.context.read_only {
            return Err(Error::CasFailed(None));
        }

        let guard = pin();

        let new_ivec = if let Some(ref n) = new {
            Some(IVec::from(&**n))
        } else {
            None
        };

        // we need to retry caps until old != cur, since just because
        // cap fails it doesn't mean our value was changed.
        loop {
            let pin_guard = pin();
            let (mut path, cur) = self
                .get_internal(key.as_ref(), &guard)
                .map_err(|e| e.danger_cast())?;

            if old != cur.map(|v| &*v) {
                return Err(Error::CasFailed(
                    cur.map(|c| PinnedValue::new(c, pin_guard)),
                ));
            }

            let mut subscriber_reservation =
                self.subscriptions.reserve(&key);

            let (leaf_frag, leaf_ptr) = path.pop().expect(
                "get_internal somehow returned a path of length zero",
            );

            let (node_id, encoded_key) = {
                let node: &Node = leaf_frag.unwrap_base();
                (node.id, prefix_encode(&node.lo, key.as_ref()))
            };
            let frag = if let Some(ref n) = new_ivec {
                Frag::Set(encoded_key, n.clone())
            } else {
                Frag::Del(encoded_key)
            };
            let link = self
                .context
                .pagecache
                .link(node_id, leaf_ptr, frag, &guard);
            match link {
                Ok(_) => {
                    if let Some(res) = subscriber_reservation.take() {
                        let event = if let Some(n) = new {
                            subscription::Event::Set(
                                key.as_ref().to_vec(),
                                n.clone(),
                            )
                        } else {
                            subscription::Event::Del(
                                key.as_ref().to_vec(),
                            )
                        };

                        res.complete(event);
                    }

                    guard.flush();
                    return Ok(());
                }
                Err(Error::CasFailed(_)) => {}
                Err(other) => {
                    guard.flush();
                    return Err(other.danger_cast());
                }
            }
            M.tree_looped();
        }
    }
/// Compare and swap. Capable of unique creation, conditional modification,
/// or deletion. If old is None, this will only set the value if it doesn't
/// exist yet. If new is None, will delete the value if old is correct.
/// If both old and new are Some, will modify the value if old is correct.
/// If Tree is read-only, will do nothing.
///
/// # Examples
///
/// ```
/// use sled::{ConfigBuilder, Error};
/// let config = ConfigBuilder::new().temporary(true).build();
/// let t = sled::Db::start(config).unwrap();
///
/// // unique creation
/// assert_eq!(t.cas(&[1], None, Some(vec![1])), Ok(()));
/// // assert_eq!(t.cas(&[1], None, Some(vec![1])), Err(Error::CasFailed(Some(vec![1]))));
///
/// // conditional modification
/// assert_eq!(t.cas(&[1], Some(&*vec![1]), Some(vec![2])), Ok(()));
/// // assert_eq!(t.cas(&[1], Some(vec![1]), Some(vec![2])), Err(Error::CasFailed(Some(vec![2]))));
///
/// // conditional deletion
/// assert_eq!(t.cas(&[1], Some(&[2]), None), Ok(()));
/// assert_eq!(t.get(&[1]), Ok(None));
/// ```
Compare and swap. Capable of unique creation, conditional modification,
or deletion. If old is None, this will only set the value if it doesn't
exist yet. If new is None, will delete the value if old is correct.
If both old and new are Some, will modify the value if old is correct.
If Tree is read-only, will do nothing.
Compare and swap . Capable of unique creation conditional modification or deletion . If old is None this will only set the value if it doesn ' t exist yet . If new is None will delete the value if old is correct . If both old and new are Some will modify the value if old is correct . If Tree is read - only will do nothing .
pub fn cas<K: AsRef<[u8]>>(
        &self,
        key: K,
        old: Option<&[u8]>,
        new: Option<Value>,
    ) -> Result<(), Option<PinnedValue>> {
        trace!("casing key {:?}", key.as_ref());
        let _measure = Measure::new(&M.tree_cas);
        if self.context.read_only {
            return Err(Error::CasFailed(None));
        }
        let guard = pin();
        let new_ivec = if let Some(ref n) = new {
            Some(IVec::from(&**n))
        } else {
            None
        };
        // we need to retry caps until old != cur, since just because
        // cap fails it doesn't mean our value was changed.
        loop {
            let pin_guard = pin();
            let (mut path, cur) = self
                .get_internal(key.as_ref(), &guard)
                .map_err(|e| e.danger_cast())?;
            if old != cur.map(|v| &*v) {
                return Err(Error::CasFailed(
                    cur.map(|c| PinnedValue::new(c, pin_guard)),
                ));
            }
            let mut subscriber_reservation =
                self.subscriptions.reserve(&key);
            let (leaf_frag, leaf_ptr) = path.pop().expect(
                "get_internal somehow returned a path of length zero",
            );
            let (node_id, encoded_key) = {
                let node: &Node = leaf_frag.unwrap_base();
                (node.id, prefix_encode(&node.lo, key.as_ref()))
            };
            let frag = if let Some(ref n) = new_ivec {
                Frag::Set(encoded_key, n.clone())
            } else {
                Frag::Del(encoded_key)
            };
            let link = self
                .context
                .pagecache
                .link(node_id, leaf_ptr, frag, &guard);
            match link {
                Ok(_) => {
                    if let Some(res) = subscriber_reservation.take() {
                        let event = if let Some(n) = new {
                            subscription::Event::Set(
                                key.as_ref().to_vec(),
                                n.clone(),
                            )
                        } else {
                            subscription::Event::Del(
                                key.as_ref().to_vec(),
                            )
                        };
                        res.complete(event);
                    }
                    guard.flush();
                    return Ok(());
                }
                Err(Error::CasFailed(_)) => {}
                Err(other) => {
                    guard.flush();
                    return Err(other.danger_cast());
                }
            }
            M.tree_looped();
        }
    }
pub fn cas < K : AsRef < [ u8 ] > > ( & self , key : K , old : Option < & [ u8 ] > , new : Option < Value > , ) -> Result < ( ) , Option < PinnedValue > > { trace ! ( " " key as_ref ( ) ) ; let _measure = Measure :: new ( & M . tree_cas ) ; if self . context . read_only { return Err ( Error :: CasFailed ( None ) ) ; } let guard = pin ( ) ; let new_ivec = if let Some ( ref n ) = new { Some ( IVec :: from ( & * * n ) ) } else { None } ; // we need to retry caps until old != cur, since just because // cap fails it doesn't mean our value was changed. loop { let pin_guard = pin ( ) ; let ( mut path , cur ) = self . get_internal ( key . as_ref ( ) , & guard ) . map_err ( | e | e . danger_cast ( ) ) ? ; if old != cur . map ( | v | & * v ) { return Err ( Error :: CasFailed ( cur . map ( | c | PinnedValue :: new ( c , pin_guard ) ) , ) ) ; } let mut subscriber_reservation = self . subscriptions . reserve ( & key ) ; let ( leaf_frag , leaf_ptr ) = path . pop ( ) . expect ( " " , ) ; let ( node_id , encoded_key ) = { let node : & Node = leaf_frag . unwrap_base ( ) ; ( node . id , prefix_encode ( & node . lo , key . as_ref ( ) ) ) } ; let frag = if let Some ( ref n ) = new_ivec { Frag :: Set ( encoded_key , n . clone ( ) ) } else { Frag :: Del ( encoded_key ) } ; let link = self . context . pagecache . link ( node_id , leaf_ptr , frag , & guard ) ; match link { Ok ( _ ) => { if let Some ( res ) = subscriber_reservation . take ( ) { let event = if let Some ( n ) = new { subscription :: Event :: Set ( key . as_ref ( ) . to_vec ( ) , n . clone ( ) , ) } else { subscription :: Event :: Del ( key . as_ref ( ) . to_vec ( ) , ) } ; res . complete ( event ) ; } guard . flush ( ) ; return Ok ( ( ) ) ; } Err ( Error :: CasFailed ( _ ) ) => { } Err ( other ) => { guard . flush ( ) ; return Err ( other . danger_cast ( ) ) ; } } M . tree_looped ( ) ; } }
Compare and swap.
Compare and swap .
&self
key
K
old
Option<&[u8]>
new
Option<Value>
&self
key
K
old
Option<&[u8]>
new
Option<Value>
examples
```
use sled::{ConfigBuilder, Error};
let config = ConfigBuilder::new().temporary(true).build();
let t = sled::Db::start(config).unwrap().






` ` ` use sled :: { ConfigBuilder Error } ; let config = ConfigBuilder :: new () . temporary ( true ) . build () ; let t = sled :: Db :: start ( config ) . unwrap () .