Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 24, 2025

Problem

Users reported being unable to find a Rust example for multiple section types, despite examples existing for Go, Node, and Java implementations. This created confusion about whether casbin-rs supports this feature.

Reference: https://casbin.org/docs/syntax-for-models#multiple-section-types

Solution

Added a comprehensive, runnable example demonstrating multiple section types support in casbin-rs.

What's New

examples/multi_section.rs - A standalone example showing:

  • How to use multiple section types in a single model
  • Section 1 (default): Three-parameter model (subject, action, object)
  • Section 2: Two-parameter model (subject, action)
  • How to use EnforceContext to switch between sections

Updated README.md - Added "Multiple section types" entry to the examples table

Example Usage

use casbin::prelude::*;
use casbin::EnforceContext;

let e = Enforcer::new(
    "examples/multi_section_model.conf",
    "examples/multi_section_policy.csv",
).await?;

// Use default section (Section 1)
e.enforce(("alice", "read", "project1"))?;

// Use Section 2 via EnforceContext
let ctx = EnforceContext::new("2");
e.enforce_with_context(ctx, ("james", "execute"))?;

Why This Matters

This example:

  • ✅ Demonstrates that casbin-rs fully supports multiple section types
  • ✅ Provides parity with other Casbin language implementations
  • ✅ Includes clear documentation on how policies and matchers work
  • ✅ Works with both tokio and async-std runtimes

Users can now easily discover and understand how to use multiple section types in their Rust projects.

Original prompt

This section details on the original issue you should resolve

<issue_title>rust multiple section types example</issue_title>
<issue_description>I found multiple section types example with go、node 、java. but can't find rust, does rust support multiple section type?
https://casbin.org/docs/syntax-for-models#multiple-section-types</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Copilot AI and others added 3 commits October 24, 2025 17:26
- Created standalone example in examples/multi_section.rs
- Added entry to README.md examples table
- Example demonstrates both section 1 (3-param) and section 2 (2-param) models

Co-authored-by: nomeguy <[email protected]>
Corrected comments to accurately describe the policy rules and how the matcher evaluates them

Co-authored-by: nomeguy <[email protected]>
Changed 'action' to 'permission' and 'role' to 'permission' for accuracy. The second field in the policy represents permission levels (admin/user), not actions (read/write)

Co-authored-by: nomeguy <[email protected]>
Copilot AI changed the title [WIP] Add Rust example for multiple section types Add Rust multiple section types example Oct 24, 2025
Copilot AI requested a review from nomeguy October 24, 2025 17:34
Copilot finished work on behalf of nomeguy October 24, 2025 17:34
@nomeguy nomeguy marked this pull request as ready for review October 24, 2025 17:43
@github-actions
Copy link

Benchmark for 2bf5287

Click to view benchmark
Test PR Benchmark Master Benchmark %
b_benchmark_rbac_model_large 9.4±0.04ms 10.2±0.03ms -7.84%
benchmark priority model 4.1±0.09µs 4.1±0.05µs 0.00%
benchmark_abac_model 2.5±0.03µs 2.5±0.03µs 0.00%
benchmark_basic_model 3.6±0.05µs 3.6±0.03µs 0.00%
benchmark_key_match 7.2±0.07µs 7.3±0.19µs -1.37%
benchmark_raw 0.0±0.00ns 0.0±0.00ns NaN%
benchmark_rbac_model 5.9±0.09µs 6.0±0.05µs -1.67%
benchmark_rbac_model_medium 788.9±8.58µs 850.8±15.24µs -7.28%
benchmark_rbac_model_with_domains 5.8±0.09µs 5.7±0.08µs +1.75%
benchmark_rbac_with_deny 8.1±0.10µs 8.3±0.11µs -2.41%
benchmark_rbac_with_resource_roles 4.3±0.06µs 4.3±0.08µs 0.00%
benchmark_role_manager_large 4.3±0.07ms 4.3±0.01ms 0.00%
benchmark_role_manager_medium 271.1±2.47µs 269.1±2.27µs +0.74%
benchmark_role_manager_small 84.0±1.42µs 92.1±1.32µs -8.79%

@github-actions
Copy link

Benchmark for 2bf5287

Click to view benchmark
Test PR Benchmark Master Benchmark %
b_benchmark_rbac_model_large 9.9±0.02ms 9.7±0.10ms +2.06%
benchmark priority model 4.1±0.09µs 4.1±0.04µs 0.00%
benchmark_abac_model 2.5±0.02µs 2.5±0.04µs 0.00%
benchmark_basic_model 3.6±0.03µs 3.6±0.04µs 0.00%
benchmark_key_match 7.5±0.10µs 7.4±0.09µs +1.35%
benchmark_raw 0.0±0.00ns 0.0±0.00ns NaN%
benchmark_rbac_model 6.1±0.24µs 5.9±0.10µs +3.39%
benchmark_rbac_model_medium 823.2±8.19µs 800.3±8.47µs +2.86%
benchmark_rbac_model_with_domains 5.8±0.04µs 5.7±0.07µs +1.75%
benchmark_rbac_with_deny 8.3±0.11µs 8.2±0.06µs +1.22%
benchmark_rbac_with_resource_roles 4.3±0.06µs 4.3±0.04µs 0.00%
benchmark_role_manager_large 4.4±0.03ms 4.2±0.01ms +4.76%
benchmark_role_manager_medium 294.5±6.77µs 267.3±3.22µs +10.18%
benchmark_role_manager_small 88.0±0.88µs 85.4±1.84µs +3.04%

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

rust multiple section types example

3 participants