rasengan 0.2.3

Circular buffer with overwrite on overflow.
Documentation
  • Coverage
  • 0%
    0 out of 4 items documented0 out of 0 items with examples
  • Size
  • Source code size: 16.61 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 980.38 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 8s Average build duration of successful builds.
  • all releases: 8s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • tauseefk/rasengan
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • tauseefk

Rasengan

Minimal circular buffer implementation. Allows overwriting data once the buffer is full. Only allows reading data once.

Write with wrap-around

In this approach W and R are incremented indefinitely (until overflow), capacity is enforced by r = R % capacity and w = W % capacity.

                                                                 
           W       R                                             
           │       │                                             
       ╔═══▼═══╦═══▼═══╦═══════╦═══════╦───────┬───────┐         
       ║       ║       ║       ║       ║       │       │▐▌       
       ║       ║       ║       ║       ║       │       │▐▌       
       ╚═══▲═══╩═══════╩═══════╩═══╤═══╩───────┴───────┘▐▌       
        ▀▀▀│▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀│▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▘       
           └─── Capacity = 4 ──────┘                             
                                                                 
       W < R // buffer has no unread values                      
       read() -> panic("Nothing to read here")                   
                                                                 
           W       R                                             
           │       │                                             
       ╔═══▼═══╦═══▼═══╦═══════╦═══════╦───────┬───────┐         
       ║       ║       ║       ║       ║       │       │▐▌       
       ║       ║   5   ║       ║       ║       │       │▐▌       
       ╚═══▲═══╩═══════╩═══════╩═══╤═══╩───────┴───────┘▐▌       
        ▀▀▀│▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀│▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▘       
           └───────────────────────┘                             
                                                                 
       inc(W); write(5); // increment W before writing           
                                                                 
           w       R                       W                     
           │       │                       │                     
       ╔═══▼═══╦═══▼═══╦═══════╦═══════╦───▼───┬───────┐         
       ║       ║       ║       ║       ║       │       │▐▌       
       ║   8   ║   5   ║   2   ║   4   ║       │       │▐▌       
       ╚═══▲═══╩═══════╩═══════╩═══╤═══╩───────┴───────┘▐▌       
        ▀▀▀│▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀│▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▘       
           └───────────────────────┘                             
                                                                 
       inc(W); write(2); inc(W); write(4); inc(W); write(8);     
       w = W % capacity // write with wrap-around                
                                                                 
                                                                 

Fork in the road

Depending on the sequence of operations there are two alternatives:

Read with wrap-around

In this case the reader resumes, so no unread values were overwritten.

                                                                                    
                                                                 
           w       r                       W       R             
           │       │                       │       │             
       ╔═══▼═══╦═══▼═══╦═══════╦═══════╦───▼───┬───▼───┐         
       ║       ║       ║       ║       ║       │       │▐▌       
       ║   8   ║   5   ║   2   ║   4   ║       │       │▐▌       
       ╚═══▲═══╩═══════╩═══════╩═══╤═══╩───────┴───────┘▐▌       
        ▀▀▀│▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀│▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀        
           └───────────────────────┘                             
                                                                 
       read() -> 2; inc(R);                                      
       read() -> 4; inc(R);                                      
       read() -> 8; inc(R);                                      
       r = R % capacity // read with wrap-around                 
       w < R // buffer has no unread values                      
       read() -> panic("Nothing to read here.")                  

                                                                 

Write with overwrites

In this case the reader is still busy and the writer overwrites unread values. The read pointer is then moved to the least recent values in the buffer.

                                                                          
                                                           ╷              
                             ├───────╴capacity - 1╶────────┤              
                         w  R,r                            W              
                         │   │                             │              
       ╔═══════╦═══════╦═▼═══▼═╦═══════╦───────┬───────┬───▼───┐          
       ║       ║       ║       ║       ║       │       │       │▐▌        
       ║   8   ║   7   ║  10   ║   4   ║       │       │       │▐▌        
       ╚═══▲═══╩═══════╩═══════╩═══╤═══╩───────┴───────┴───────┘▐▌        
        ▀▀▀│▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀│▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀         
           └───────────────────────┘                                      
                                                                          
       inc(W); write(7); inc(W); write(10);                               
       W - R = capacity - 1                                               
       // unread values at capacity,                                      
       // next write overwrites unread values                             
       inc(R) // move to least recent value in the buffer                 
                                                                          
                                                                          
                                                                          
                                                                          
                           w      R,r                      W              
                           │       │                       │              
       ╔═══════╦═══════╦═══▼═══╦═══▼═══╦───────┬───────┬───▼───┐          
       ║       ║       ║       ║       ║       │       │       │▐▌        
       ║   8   ║   7   ║  10   ║   4   ║       │       │       │▐▌        
       ╚═══▲═══╩═══════╩═══════╩═══╤═══╩───────┴───────┴───────┘▐▌        
        ▀▀▀│▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀│▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀         
           └───────────────────────┘                                      
                                                                          
       FINAL CONFIGURATION